#if in .pde

Hi,

I can't get this to compile properly (strip down version of my real code..):

#if (TEST_MODE)
void UselessFunction();
#endif


void setup()
{
}

void loop()
{
  
}

#if (TEST_MODE)
void UselessFunction()
{
  
}
#endif

Removing both #if does compile:

void setup()
{
}

void loop()
{
  
}

I get the same result on arduino 19 and 20.

seem like I hit this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1261506776

I wonder what would be the strategy to use such enable/disable code with arduino IDE...

it always keep adding the function definition regardless of the #define value as well...

Mart

This works:

[MAIN TAB]

void setup() {
  test(); //call test from the Test tab
}

void loop() {/*nothing to loop*/}

[Test TAB]

#define TEST 
#ifdef TEST
void test() {Serial.println("TEST");}
#else
void test(){}
#endif

You simply need to put such things in a different tab. I guess the pre-preprocessor of the IDE messes up preprocessor uses like this. :frowning:

#if what? Do you mean #ifdef?

AlphaBeta,

I am trying to use only one .pde (main sketch) and use classes for everything else (.h & .cpp) for linux UIT purposes. I think that's where it get tricky with the arduino pre-processing...

thanx for opening my eyes :slight_smile:

The ardunio pre-processor does some things to detect the first C statement in your .pde file, and it gets confused by some pre-processor statements. Move a piece of real C code before your attempts to use #if, and you should be fine. (or add a dummy declaration)

byte foo;

#if defined(TEST_MODE)
void UselessFunction();
#endif


void setup()
{}

void loop()
{}

#if defined(TEST_MODE)
void UselessFunction()
{}
#endif

I think maybe this is even closer to what OP wants:

//#define TEST_MODE
byte foo;

#if defined(TEST_MODE)
void UselessFunction();
#else
void UselessFunction(){}
#endif


void setup()
{UselessFunction();}

void loop()
{}

#if defined(TEST_MODE)
void UselessFunction()
{Serial.println("TEST");}
#endif

IMHO I think it's cleaner to simply add a new tab, or #include a .c :slight_smile: