Go Down

Topic: Need to set a define in a sketch so a library can compile correctly. (Read 374 times) previous topic - next topic

teckel

I'm fairly competent in programming including the Arduino IDE and have written several Arduino libraries.  For compile time options that are already built into the library, I want to set those options with a #define in the sketch (not the library).  But, for the life of me I can't figure out how I should be doing this.  I've got to imagine it's possible.  Basically, I want to do something like this:

Code: [Select]

#include <CoolLibrary.h>

#define COOLLIBRARY_SET_SOMETHING

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


And the #define COOLLIBRARY_SET_SOMETHING will allow me (at compile time) to choose different compile time options in the library.  Obviously, the library itself will be something like this:

Code: [Select]

/* Some library stuff */

#if defined (COOLLIBRARY_SET_SOMETHING)
  somevalue = 2;
#else
  somevalue = 4;
#endif

/* More library stuff */


So, any way to set a define in a sketch so that the library knows about it.  Just adding it in the sketch (before or after the library include) doesn't work.  Any trick?  Just not possible?

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

PaulS

The library is a compilation unit. Only #define statements in that compilation unit have any affect on the resulting code that is compiled. So, you can't put a #define in one compilation unit (the sketch) and expect it to do something in another compilation unit.

PeterH

To do what you want, you would need to pass the pre-processor definition to the compiler. Many development environments enable you to do this. The Arduino IDE does not.

As I see it, your options are to change the library source code to achieve what you want without needing you to pass in preprocessor definitions, or use a different IDE.
I only provide help via the forum - please do not contact me for private consultancy.

teckel


To do what you want, you would need to pass the pre-processor definition to the compiler. Many development environments enable you to do this. The Arduino IDE does not.

As I see it, your options are to change the library source code to achieve what you want without needing you to pass in preprocessor definitions, or use a different IDE.


Thanks, it seemed that way.  What gave me hope was by adding the define above the library include I got errors like it was trying to do what I wanted.

So, is there anyway in a sketch level that one can control how the library is compiled?  Basically, looking for a method of setting switches that control what is compiled.  For example, a normal mode and a "small" mode where the features are reduced but the footprint is much smaller.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

Go Up