1

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 <TinyWireM.h>    // 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 <Wire.h>         // This is the Wire library for the Uno.
#endif

void setup() { }

void loop() { }

2 Answers 2

3

The problem isn't your code. There is a 2012 bug report on this issue in Google Code.

Once you start getting bitten by the non-standard behavior of the Arduino IDE, you've outgrown it and the only real remedy is to move up to real tools that work as you'd expect. A few of the choices include:

  • Eclipse is a good choice; it can take a little patience and searching to get it set up right, but once you do it's a solid development environment you can stay with for a long time. It runs everywhere Java does.

  • Atmel Studio is well thought of. It's proprietary, not free [?], and only for windows.

  • Apple's Xcode can be configured to use the avr-gcc toolset. (Even thought it's right here on my system, I'm happy with Eclipse and haven't tried it).

  • Likewise, you can use Microsoft Visual Studio (the linked article is just one of many).

  • Command line: You can get down and dirty with your favorite editor, the gcc toolset for AVR, and bang away at your keyboard. Not sexy, but you'll learn everything that's going on between your code the board.

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.