I created a library and it is configureable depending on the board being used. Is there a way to configure the library using #define macros? In other words is there a way to reference a header file from within the library that is in the current sketch that I am working on?
A #define macro ahead of the #include macro for the library should be sufficient if the library uses #if and #elif. Your way would be clever if it was possible but would only save one line.
Is there a way to configure the library using #define macros?
Yes, under the following conditions:
- The #define directives come before the #include directive for the library header file.
- All code controlled by those macros is in the header file, not the source file (.cpp/.c).
However, that might not even be necessary:
it is configureable depending on the board being used.
If that's all the configuration information you need, you may be able to determine which board is used via automatically defined global macros. The compiler defines a macro for the microcontroller model being compiled for. For example, if you are compiling for a board that uses the ATmega328P, then the AVR_ATmega328P will be defined. You can find the full list of AVR macro names here:
Presumably there will be something of the sort for other architectures.
The Arduino IDE also defines a board-specific macro. So if you have Tools > Board > Arduino/Genuino Uno selected, the ARDUINO_AVR_UNO macro will be defined. This is defined via the build.board property in boards.txt.
is there a way to reference a header file from within the library that is in the current sketch that I am working on?
This is a much-requested feature but the Arduino organization has been very against providing anything like this because they think it will lead to unnecessarily complex library interfaces. You can specify the full path to a file in the #include directive but that makes the library very non-portable.
Thank you for explaining where this is documented.
Thanks for the info.
You’re welcome. I’m happy to provide more details if needed.