Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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...

share|improve this question

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.

share|improve this answer
    
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 at 14:35
    
V3 is comming. That should make it easy :-) – jantje Mar 7 at 15:29

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.

share|improve this answer
    
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 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 at 11:30

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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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