Preprocessor #ifdef guard and #include not working as expected

Hi,

I’m a hobbyist migrating from Arduino IDE to PIO and encountered an unexpected error related to #include guarded by #ifdef. Using Arduino IDE, the following sketch compiles without problems. I searched cpp reference, stackexchange etc. because I thought I just lack basic C++ knowledge. However, I could not find any hint. My complete main.cpp:

#ifdef __AVR_ATmega328__
#include <RF24.h>
#elif STM32_MCU_SERIES && STM32_MCU_SERIES == STM32_SERIES_F1
#include <RF24_STM32.h>
#endif /* ifdef __AVR_ATmega32 */
int main() {
 RF24 radio(9,10);
}

The error message:

Verbose mode can be enabled via `-v, --verbose` option
Collected 48 compatible libraries
Looking for dependencies...
Library Dependency Graph
|-- <RF24_STM32>
|   |-- <SPI> v1.0
|   |-- <RF24> v1.3.0
|   |   |-- <SPI> v1.0
|-- <RF24> v1.3.0
|   |-- <SPI> v1.0
Compiling .pioenvs/nanoatmega328/src/main.o
Archiving .pioenvs/nanoatmega328/lib/libRF24.a
Indexing .pioenvs/nanoatmega328/lib/libRF24.a
Compiling .pioenvs/nanoatmega328/lib/RF24_STM32/RF24_STM32.o
/home/piouser/.platformio/lib/RF24_STM32/RF24_STM32.cpp:17:17: error: no matching function for call to 'SPIClass::SPIClass(int)'
SPIClass SPI_2(2);
^
/home/piouser/.platformio/lib/RF24_STM32/RF24_STM32.cpp:17:17: note: candidates are:
In file included from /home/piouser/.platformio/lib/RF24_STM32/RF24_config_STM32.h:77:0,
from /home/piouser/.platformio/lib/RF24_STM32/RF24_STM32.cpp:10:
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note: constexpr SPIClass::SPIClass()
class SPIClass {
^
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note:   candidate expects 0 arguments, 1 provided
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note: constexpr SPIClass::SPIClass(const SPIClass&)
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note:   no known conversion for argument 1 from 'int' to 'const SPIClass&'
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note: constexpr SPIClass::SPIClass(SPIClass&&)
/home/piouser/.platformio/packages/framework-arduinoavr/libraries/__cores__/arduino/SPI/src/SPI.h:156:7: note:   no known conversion for argument 1 from 'int' to 'SPIClass&&'
*** [.pioenvs/nanoatmega328/lib/RF24_STM32/RF24_STM32.o] Error 1
========================== [ERROR] Took 0.74 seconds ==========================
Environment nanoatmega328               [ERROR]
========================== [ERROR] Took 0.74 seconds ==========================

Content of platformio.ini

[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_flags = -D__AVR_ATmega328__

RF24_STM32.h is included although the #ifdef does not match. When compiling this file as an ino using Arduino IDE everything works as expected, i.e. only one #include at a time is used.
Not sure if it is expected behavior or just my fault or a bug. Would be great if someone can point me in the right direction.

Best

Try to add to platformio.ini lib_ldf_mode = chain+

Or, just ignore RF24_STM32 for AVR build environment:

[env:nanoatmega328]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_flags = -D__AVR_ATmega328__
lib_ignore = RF24_STM32

Thanks a lot. Ignoring the lib did the trick:

lib_ignore = RF24_STM32

I’m curious, is this a bug? I’m a hobbyist but could not find any hint that the code example is non-standard code.

Best

1 Like