Build flags unexpected behavior


#1

Hi,

I have stripped this down to a minimal example, it can be found here (full code below). Basically I have some compilation pre-processors to selectively include parts of code. I have tried using both the PLATFORMIO_BUILD_FLAGS environment variable to set HIGH_LOW well as build_flags in platformio.ini. What I am seeing is the compiler generating an error for line 9 when HIGH_LOW is set to HIGH. Based on what I know about how preprocessors work this part of the code should not be included based on the value of HIGH_LOW.

Further, the error does go away if I transpose the ordering of the blocks that use #if HIGH_LOW logic. I feel that only one of these blocks should be executed as the == comparison operator is being used, so I’m not sure how this could occur. Any insight that could be provided would be greatly appreciated.

#if HIGH_LOW==HIGH
void emptyFun() {
}
#endif

#if HIGH_LOW==LOW
#include <Adafruit_BNO055.h>;
void displaySensorOffsets(const adafruit_bno055_offsets_t &calibData)
{
}
#endif

void setup() {
}

void loop() {
}

#2

This is an error regarding the INO preprocessing. Your example works by simply renaming SAMD21.ino -> SAMD21.cpp.

I had a look at the pre-processor output of the file and it definitely looks weird. (Add -E to the build_flags, run pio run -e samd21b and open .pioenvs\samd21b\src\SAMD21.ino.cpp.o). It does a function declaration but without ever including the header file for it…

void emptyFun();
void displaySensorOffsets(adafruit_bno055_offsets_t &calibData); //no #include "Adafruit_BNO055.h" before this or the content of the header file!
void setup();
void loop();
# 5 "C:/Users/Maxi/Desktop/platformio-build-flags-error/src/SAMD21.ino"
void emptyFun() {

}
# 29 "C:/Users/Maxi/Desktop/platformio-build-flags-error/src/SAMD21.ino"
void setup() {

}



void loop() {

}

This also happens regardless of adding lib_ldf_mode = chain+ or deep or lib_deps = Adafruit_BNO055.


#3

Oh well, another way of making the INO file working is by not using another Arduino macro inside the -D definitions. doing build_flags = -DHIGH_LOW=1 instead of build_flags = -DHIGH_LOW=HIGH makes it work, too.


#4

@maxgerhardt thank you so much for taking the time to help me. I have a deep admiration for the time highly-skilled workers are often wiling to spend addressing the problems of others - you are no exception in this case.

As you described, renaming the project file from .ino -> .cpp fixed this issue, although I am not unable to achieve success using 1 instead of HIGH in the build_flags. Since I do have a working solution, and the complexity of the problem is above my knowledge domain, I do not intend to pursue this much further.

The -E switch to pio is certainly something new that I learned today, so I thank you for this as well.

Cheers,

Dan