Pages: [1]   Go Down
Author Topic: Need to set a define in a sketch so a library can compile correctly.  (Read 355 times)
0 Members and 1 Guest are viewing this topic.
Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 514
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
/* 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
Logged

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 642
Posts: 50366
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Toledo, OH
Offline Offline
God Member
*****
Karma: 36
Posts: 514
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Pages: [1]   Go Up
Jump to: