#ifdef / #include CPP issue I'm developing a sketch which I need to run on an Uno (for debug) and a Gemma (as the target).  Adafruit has their own Wire library (TinyWireM) which I was planning on using on the Gemma.  Thus, I was trying to use #if's to automatically include the correct library for the given target board.  However, it appears that the TinyWireM include files is always included even if the AVR_ATtiny85  macro symbol is undefined (I get many undefined symbols reported inside the Arduino\libraries\TinyWireM-master\USI_TWI_Master.cpp file).  If I comment out the TinyWireM.h include statement (inside the #if define statement), then it compiles fine.  Code example below.

So why isn't this include statement being skipped?  I'm at a loss.  Anyone have any thoughts?   

Thx all.

Mike

#undef __AVR_ATtiny85__        // Debug only - making sure not defined.

#if defined (__AVR_ATtiny85__)
  #include &lt;TinyWireM.h&gt;    // Seems this file is still being included and
                          // must comment out this line when compiling 
                          // code for an UNO.  This should not be necessary 
                          // given __AVR_ATtiny85__ is not defined.
#else
  #include &lt;Wire.h&gt;         // This is the Wire library for the Uno.
#endif

void setup() { }

void loop() { }
The Arduino IDE does its own non-standard preprocessing before the C++ preprocessor/compiler is invoked. One of the main purposes for this is to detect #include directives which refer to libraries so it can link them properly.

My guess would be that part of this procedure ends up moving all #include lines to the start of the file. In the code you've posted, that means I think it's being moved outside the whole #if/#else structure.

In most cases, you can avoid the Arduino IDE trickery by moving things into a separate header or source file. Unfortunately though, doing that means it may not link the libraries properly. As such, the easiest solution may be to switch to a 3rd party IDE which has Arduino support (e.g. Eclipse).