0

I have a library the uses conditional compilation based on the preprocessor variables. The library is a separate .cpp object file with a header.

How can I pass preprocessor variables to the compilation of the library? Standard #define in a sketch file wouldn't work, since their live will be limited to my sketch object file(s), not the library's.

Normally it is solved by the ./configure script and/or the custom make process. How can I do it on Arduino?

Supplying a separate compilation/source code of the library for all 2^8 combinations of the preprocessor switches doesn't seem feasible...

3

The problem here is that the Arduino IDE is not setup for doing this. Any real IDE has the possibility to set defines.

If you want your library to be used by arduino IDE users you will have to go to dirty routes like the #include and config.h approach already mentioned. If I'm not mistaken arduino IDE compiles libraries with the full include path so you can have the config.h next to your sketch.ino file.

If the library is not intended for arduino IDE users I would advice using a real IDE like my Arduino Eclipse Plugin.

  • I already use your excellent Eclipse Plugin. (I tested several other IDEs, but your proved to give me the best experience). But I'd prefer my library to compatible with the standard IDE if I had a choice. Setting the Eclipse with your plugin took me a half a day and two attempts... Not everyone will be able to do that. – Adam Ryczkowski Mar 7 '16 at 14:35
  • V3 is comming. That should make it easy :-) – jantje Mar 7 '16 at 15:29
1

There's two basic methods:

  1. Provide a config.h file with the options in that the user modifies
  2. Place the options on the command line with -D

A decent IDE allows you to modify the compilation command line to add options.

My IDE, UECIDE, allows the creation of menu entries that set library options.

  • Ad 1. How to tell the library where is my config.h? When library's object file gets compiled, it doesn't know the location of my project's path. Or am I wrong? – Adam Ryczkowski Mar 7 '16 at 11:29
  • The config.h is in the library. Depending on the IDE it may be possible to have it in the sketch folder and it'll find it there, but I am not sure what order of include paths the Arduino IDE uses. – Majenko Mar 7 '16 at 11:30
0

Another solution: Rename the library's .cpp file into another .hpp, and let the user include it in exactly one object file of his.

User would configure the library in lib_customization.h included before every #include<lib.h>. User would also need to include an extra source file object_file_that_defines_lib.cpp which includes library definitions.

Sketch.ino:

#include"lib_customization.h"
#include<lib.h>

void loop() {}
void setup() {}

lib_customization.h:

#define LIB_INCLUDE_FEATURE_A
#define LIB_INCLUDE_FEATURE_B

object_file_that_defines_lib.cpp:

#include"lib_customization.h"
#include<lib_definitions.h>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.