Arduino IDE compiler bug?

I think I found some kind of bug of the compiler. This code doesn't compile (Error: 'Serial' was not declared in this scope)

//#define BOARD_V1 
#define BOARD_V2
 
#ifdef BOARD_V1
  int contacts[6] = {0, 2, 3, 4, 5, 1};
#else
  int contacts[6] = {21, 19, 18, 24, 3, 20};
#endif

void setup()
{
  Serial.begin(9600);  
}

void loop()
{
 ;
}

However, if I change "#ifdef" as "ifndef", or I just comment the line under it, It compiles fine. Tested with Arduino IDE 0022.

Regards.

I get the same error.

Declaring something (eg. int dummy) before the #ifdef statement solves the error.

ZhordeR:
Declaring something (eg. int dummy) before the #ifdef statement solves the error.

That would be considered a workaround, right?

tastewar:
That would be considered a workaround, right?

Yes, that's right.

Ya. That's an IDE bug. The IDE looks for the first character in the sketch that is not whitespace, comment, or preprocessor directive, and that's the point where it inserts it's Wprogram.h include and function prototypes. If you do a verbose Verify and then look at the cpp file that is generated, this is what it looks like:

//#define BOARD_V1 
#define BOARD_V2
 
#ifdef BOARD_V1
  #include "WProgram.h"
void setup();
void loop();
int contacts[6] = {0, 2, 3, 4, 5, 1};
#else
  int contacts[6] = {21, 19, 18, 24, 3, 20};
#endif

void setup()
{
  Serial.begin(9600);  
}

void loop()
{
 ;
}

As you can see, the #include gets put inside the #ifdef, and thus doesn't get included by the gcc preprocessor, and thus the source file has no idea what Serial is (since it's included by WProgram.h)

Hi,

I faced the same bug.

Thank you for the work-around :wink:

Would moving your pre-processor directives into a .h file solve the problem?

I'm trying to use this in Arduino IDE 1.0

// select the screen to use
// set OUTPUT_TYPE to 1 if you want to use LCD
// set OUTPUT_TYPE to 2 if you want to use LED
// set OUTPUT_TYPE to 3 if you want to use VGA
// set OUTPUT_TYPE to 4 if you want to use TV-OUT
#define OUTPUT_TYPE 1               // use 1 2 3 or 4

// user shouldn't change anything below this line

#if OUTPUT_TYPE == 1
    #include  <LiquidCrystal.h>
#elif OUTPUT_TYPE == 2
    #include "font_7X5.h"
    #include <HT1632.h>
#elif OUTPUT_TYPE == 3
    #include <arduino_uvga.h>
    #include <conio.h>
#elif OUTPUT_TYPE == 4
    #include <TVout.h>
    #include <font8x8ext.h>
#endif

But it doesn't work, all libraries are loaded nevertheless

(Please don't cross-post replies to other threads!)

Re-read this thread carefully, especially jraskell's comment.

Try to change OUTPUT_TYPE to 2 and it shouldn't include ANY library.

Think I made a mistake, it does work in Arduino IDE 1.0 without the dummy.

Sorry about that :blush: