Does #ifdef / #if defined() not work on Arduino IDE?

Can someone tell me if this works for them?

void setup()
{
  Serial.begin(9600);
  
  while(!Serial);
  
  Serial.println("Test...");
}

void loop()
{

}

#ifdef FOO
byte fileReadln(File myFile, char *buffer, byte count)
{
}
#endif

Even though the fileReadln() should not compile in, it errors out looking for "File" as if it is still processing all the function headers. I was trying to #ifdef all my support for SD and Ethernet, but I ran in to this issue.

Is it just mine?

Hi,

maybe it happens because the arduino IDE changes your sourcecode to get it agreeable for AVR-GCC.

After compiling your project you can find a cpp file of that sketch in your temp dir. Maybe taking a look to it makes it clear.

Some time ago, I had similar issues. This link help me to solve it: http://www.a-control.de/arduino-fehler/?lang=en

No, it is not just you. I think it is an error.

If you do this in the code, it is not a problem. It has to do with the automatic function prototyping. It seems to processes all function declarations.

I suspect everything gets compiled, it’s the linker that won’t add the function if it’s not referenced in the main code.

Lefty

Automatic prototypes - I wondered how that worked. I will check these links. Hopefully there is a way to work around this.

And this fails with very bizarre errors… If I move the “byte mac” declaration outside of the #ifdef, it works.

#if defined(FOO)
#include <Spi.h>
#include <Ethernet.h>
byte mac[] PROGMEM = { 0x2A, 0xA0, 0xD8, 0xFC, 0x8B, 0xEE };
#endif

void setup()
{
  Serial.begin(9600);
  while(!Serial);  
  Serial.println("Test...");
}

void loop()
{
}

Fantastic! The first link helped explain and resolve it. I am just inserting this at the top of my code and now it works fine.

// BOF preprocessor bug prevent - insert me on top of your arduino-code

if 1

__asm volatile ("nop");

endif

d'oh! I wish I had found this before I wrote a mini-cpp to pre-process my own #ifdefs to get around this problem. The explanation web-page has been disabled, but I found it cached at:

http://webcache.googleusercontent.com/search?q=cache:http://www.a-control.de/arduino-fehler/?lang=en

Scroll down to get to the english version.