chipKIT® Development Platform

Inspired by Arduino™

BUG BACK— #ifdef / #if defined #include Statement

Created Fri, 05 Aug 2011 19:45:08 +0000 by avenue33


avenue33

Fri, 05 Aug 2011 19:45:08 +0000

Hi!

I'm trying this code on MPIDE:

1— #include "WProgram.h"
2—
3— #if defined(__AVR__)    // Arduino Case ---
4— #include "NewSoftSerial.h" //  <=== problem (*)
5— NewSoftSerial mySerial(2, 3); // RX, TX
6—
7— #elif defined(__PIC32MX__)    // chipKIT Case ---
8— HardwareSerial mySerial = Serial1;
9— 
10— #else  // check
11— #error Non defined board
12— #endif 
13—
14— void setup() {
15—  mySerial.begin(19200);
16—  mySerial.print("Hello !");
17— }
18—
19— void loop() {
20— }

With board = Arduino Uno, compiling successful.

With board = UNO32, error message [color=#FF0000]Error running. /Users//Documents/Arduino/_Projets/libraries/NewSoftSerial/NewSoftSerial.cpp:37:27: fatal error: avr/interrupt.h: No such file or directory compilation terminated.[/color] The #include statement in line 4— seems to be taken into account even though the #if statement in line 3— returns false. Adding

#error error

between lines 3— and 4— doesn't raise any error.

If I change the line 4— into

4— // #include "NewSoftSerial.h"

Choosing board = UNO32, compiling successful.

What's wrong with my code :?: Thank you for your help.


GeneApperson

Fri, 05 Aug 2011 20:40:05 +0000

I just ran the following sketch:

#include <WProgram.h>

void setup() {
pinMode(13, OUTPUT);
}

void loop() {

#if defined(AVR) digitalWrite(13,HIGH); delay(500); #elif defined(PIC32MX) digitalWrite(13, HIGH); // set the LED on delay(100);
digitalWrite(13, LOW); // set the LED off delay(100); digitalWrite(13, HIGH); // set the LED off delay(100); #endif digitalWrite(13, LOW); // set the LED off delay(500); // wait for half a second }

Which gave the expected fast double blink when run with UNO32 selected rather than the slow blink that would have occured if AVR was defined.

Running the same thing on a Duemilanove, I got the slow blink I would expect to see.

It appears that the conditionals are set up properly. I don't see anything wrong with what you are doing. Either AVR is somehow getting defined in your setup, or the preprocessor is doing the #include even though it shouldn't.

Gene Apperson Digilent


jasonk

Sat, 06 Aug 2011 18:06:06 +0000

/Users//Documents/Arduino/_Projets/libraries/NewSoftSerial/NewSoftSerial.cpp:37:27

I am guessing that the #include is causing MPIDE to pull in the NewSoftSerial.cpp library regardless of the preprocessor conditional. I think we can get around this for now by also adding the conditionals in the NewSoftSerial.cpp library file. That way, MPIDE will still try to compile the file, but it won't try to compile the AVR-specific code within the file.


avenue33

Sat, 06 Aug 2011 18:22:38 +0000

Hi Jason,

Thanks for the tip. The patch works.

I am guessing that the #include is causing MPIDE to pull in the NewSoftSerial.cpp library regardless of the preprocessor conditional.

Should a bug ticket be opened at the Github chipKIT32 repository?

Best regards,


WestfW

Sat, 06 Aug 2011 20:20:19 +0000

I am guessing that the #include is causing MPIDE to pull in the NewSoftSerial.cpp library regardless of the preprocessor conditional.

IIRC, this is a generic problem with the Arduino environment as well; not getting the automatic library searching quite right in the face of conditional compilation...


ricklon

Sun, 07 Aug 2011 22:22:03 +0000

There are several reported problems with #ifdef with the Arduino project: http://code.google.com/p/arduino/issues/list?can=2&q=ifdef&colspec=ID+Type+Status+Priority+Milestone+Owner+Summary&cells=tiles

Is this issue different than those?


Mark

Mon, 08 Aug 2011 02:19:42 +0000

This has been a long standing bug in the Arduino pre-processor. It does NOT evaluate #ifdefs at all and therefore looks for the file to include anyway. This bug has been in Arduino since at least 0012, probably from the very beginning.

Mark


avenue33

Mon, 08 Aug 2011 15:37:39 +0000

Hi!

The Arduino forum references the :arrow: bug.

As a proposed work-around, add at the very beginning of the sketch

int dummy;

#ifdef / #if defined should work properly now.

So we may consider this as solved, somehow :?

• Thread title set to "SOLVED somehow"


avenue33

Mon, 15 Aug 2011 14:00:21 +0000

The #ifdef / #if defined #include problem is back with New builds available for 20110813.

The [color=#0000FF]int dummy;[/color] work-around no longer works. :(

So the #include libraries should be modified (both .h and .cpp files) to skip all the code.

• Thread title set to "BUG BACK"