Go Down

Topic: #if strangness in the IDE (Read 1 time) previous topic - next topic

fat16lib

Apr 08, 2011, 09:02 pm Last Edit: Apr 08, 2011, 09:03 pm by fat16lib Reason: 1
Why does the IDE always compile Wire in the following sketch independent of the value of USE_WIRE?
Code: [Select]
#define USE_WIRE 0

#if USE_WIRE
#include <Wire.h>
#endif

void setup() {
 Serial.begin(9600);
 Serial.println("why does the ide do this");
}
void loop() {}


If I comment out the include like this
Code: [Select]
//#include <Wire.h>
The sketch is 762 bytes smaller and Wire is no longer in the build folder.

johnwasser

I think it's the way the Arduino IDE saves people from having to specify the libraries to link with.  It searches your sketch for includes and links with the associated library.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

frank26080115

The IDE uses RegEx to find things that look like #include <blah.h> and then looks for all compilable files inside the library, then compiles them for you. It ignores other pre-processor but it stripes out all comments before doing the RegEx search
Freelance engineer, consultant, contractor. Graduated from UW in 2013.

fat16lib

Too bad.  That makes preprocessor directives usless around #includes.

sixeyes

You could create your own header and use conditional compilation inside that.

Create myheaders.h and put your conditional compilation directives / #includes in there.

You can still have the #define in the main file. Not as clear as your example but if you conditionals are well named it wouldn't be too confusing.

Iain

Go Up