#define of a #include

Just a simple question for those who know more.

Given this code

// This construct doesn't work. Wire.h will be included no matter what.
// So 1244 bytes are always added unless the #include is commented out.

#define ADD_WIRE   false 

#if (ADD_WIRE)
#include <Wire.h> // <<< can only comment out to not include
#endif

void setup(){
}

void loop(){
}

I was surprised to find that Wire.h is always included regardless of the setting of the #define.
If it's not the right way to do this, is there a way that works?

Look at #ifdef

preprossesor math is really limited.

Like this?

//#define ADD_WIRE 

#ifdef ADD_WIRE
#include <Wire.h> // <<< can only comment out to not include
#endif

Same thing.

Are all the references to the wire library #ifdef'd out? The IDE might be smart enough to add in Wire.h if it sees any reference to the Wire library. Also, try a #undefine right before your #ifdef.

What we need is a clean button.

Try a new test script that has never had wire.h defined.

The sketch I posted is the entire sketch.

I am saying start a new one that has never had wire.h included.

Then add the code that should prevent wire.h from being included.
Then add the #define to include wire.h
Then try to uninclude wire.h

The ide might be doing some stupid magic behind teh scenes and refuses to let go of wire.h.

It looks like processing breaks the standard preprocessor. Note that the only directive mentioned in the reference is #define.

I could not even include Wire.h in a separate include file, it had to go into the main file.

I think the only thing you can do is comment it out.

A little forum search led me to this:
http://www.a-control.de/arduino-fehler/?lang=en

Does that help?

Thanks Keith, I appreciate your help.

I tried adding

/*
 * BOF preprocessor bug prevent
 * insert me on top of your arduino-code
 */
#define nop() __asm volatile ("nop")
#if 1
nop();
#endif
/*
 * EOF preprocessor bug prevent
*/

before the sketch I first posted with the same results - 1710 bytes and 466 bytes if the #include is commented out.
Unless I missed something in that example, that should be the workaround.

It appears that the precompiler makes one pass and seems to handle #includes before #defines.

It appears that the precompiler makes one pass and seems to handle #includes before #defines.

Are you looking at the .ino file or the .cpp file that the IDE constructs from the .ino file? The IDE does some "unusual" things to convert a .ino file to a .cpp file.

bHogan:
Same thing.

When I compile this sketch with 1.0.4, it fails with:

sketch_apr24a.ino: In function 'void setup()':
sketch_apr24a:7: error: 'Wire' was not declared in this scope

This indicates that Wire.h was not included.

#ifdef ADD_WIRE
#include <Wire.h>
#endif

void setup()
{
  Wire.begin();
}

void loop()
{
}

This sketch compiles without error:

#define ADD_WIRE
#ifdef ADD_WIRE
#include <Wire.h>
#endif

void setup()
{
  Wire.begin();
}

void loop()
{
}

That's what you're trying to achieve, isn't it?

Not exactly.
I'm trying to save the 1244 bytes that Wire.h adds to the sketch even when it's not defined.
The fact that it's not "compiled in enough" to satisfy Wire.begin is interesting, buy if you try the sketch as I have it in the first post,
compile it and note record the sketch size,
and then comment out the #include and note the sketch size again, you will see a 1244 byte difference.

I'm using this construct in another sketch that is close to the wall, and I'd like to avoid this overhead by setting a #define.
That's what I'm trying to achieve but it's looking unlikely.

The code I posted did not compile in Wire.h at all. However, I think what you're complaining about is actually the linker. For reasons which seem bizarre to me, the Arduino approach is to copy the sketch file and all associated code tabs, plus all the libraries and bits of the Arduino runtime framework which it references, into a temporary directory - and then compile and link everything. What's happening here is that the Arduino IDE is noticing the #include <wire.h> in your sketch, decided you are using the associated library, and including that library in the build. In theory the linker is smart enough to only link in the library functions that your code actually refers to, but in practice it seems to be pulling in quite a lot of junk in your case.

As far as I can see, the only options you have to avoid that are to comment out the #include to stop the IDE screwing up, or move to a grown up IDE.

You might be able to use a pre-preprocessor like M4 to add and remove comment tokens as necessary.

Peter, that seems to describe what is happening. For me, the question is answered and I guess I'll have to just comment out that #include in addition to setting the #define false. Thanks for everyone's help.