PlatformIO ignores #ifdef Includes

Hallo together,
i have following problem.
After a Update for Platformio the #include “file” in preprocessor #ifdef get ignored.

#ifndef DeviceDefinition_H
#define DeviceDefinition_H
#include "Arduino.h"

#define USE_MCP23017

#ifdef USE_MCP23017     
    #include "MCP23017.h"
    MCP_23017Class Device;
#endif
#ifdef USE_Gateway
    #include "Gateway.h"
    GatewayClass Device;
#endif
#ifdef USE_PCA9685   
    #include "PCA9685.h"
    PCA9685Class Device;
#endif
#endif    

Befor the update all works fine. But now it throw a “no such file” error message. If i define the Class
outside the #ifdef it works again.

I tryed to clean the project, deleted the .pio folder, changed lib_ldf_mode from chain+ to deep+.

But nothing helped.

Does anyone have the same troubles?

Thanks for help :slight_smile:

Can you post the exact error? Is it a compiler error or a VSCode intellisense error? If it’s a compiler error, what’s the full log?

------------------------------------------------------------------------------------------------------------------------------------- 
----------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 1.12.0 > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, 
olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 3.10004.200129 (1.0.4)
 - tool-esptoolpy 1.20600.0 (2.6.0)
 - toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain+, Compatibility ~ soft
Found 35 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ArduinoJson> 6.11.0
|-- <Dictionary>
|   |-- <ArduinoJson> 6.11.0
|-- <SPIFFS> 1.0
|   |-- <FS> 1.0
|-- <FS> 1.0
|-- <GlobalFunction>
|   |-- <Dictionary>
|   |   |-- <ArduinoJson> 6.11.0
|   |-- <FS> 1.0
|   |-- <ArduinoJson> 6.11.0
|   |-- <SPIFFS> 1.0
|   |   |-- <FS> 1.0
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Generating partitions .pio\build\esp32dev\partitions.bin
Archiving .pio\build\esp32dev\lib74c\libArduinoJson_ID64.a
Compiling .pio\build\esp32dev\libc99\Dictionary\Dictionary.cpp.o
Compiling .pio\build\esp32dev\lib594\FS\FS.cpp.o
Compiling .pio\build\esp32dev\lib594\FS\vfs_api.cpp.o
Compiling .pio\build\esp32dev\lib4d4\SPIFFS\SPIFFS.cpp.o
Compiling .pio\build\esp32dev\libbb3\GlobalFunction\GlobalFunction.cpp.o
lib\Dictionary\src\Dictionary.cpp: In member function 'Objects_t* 
DictionaryClass::Object_Search(uint16_t)':
lib\Dictionary\src\Dictionary.cpp:255:1: warning: control reaches end of non-void function [-Wreturn- 
type]
  }
  ^
Archiving .pio\build\esp32dev\libFrameworkArduinoVariant.a
In file included from lib\GlobalFunction\src\GlobalFunction.cpp:2:0:
lib\GlobalFunction\src\DeviceDefinition.h:9:30: fatal error: MCP23017.h: No such file or directory
 ******************************************************************
 * Looking for MCP23017.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:MCP23017.h"
* Web  > https://platformio.org/lib/search?query=header:MCP23017.h
*
******************************************************************

compilation terminated.
 *** [.pio\build\esp32dev\libbb3\GlobalFunction\GlobalFunction.cpp.o] Error 1
 Compiling .pio\build\esp32dev\FrameworkArduino\Esp.cpp.o

Thats the Error i get .

I don’t even find a reference for a library which implements MCP_23017Class. Which library do you use? Does declaring it in lib_deps help?

If not, can you share a minimal project which reproduces the problem?

Puh thats a little bit tricky.

main.cpp

 #include "Arduino.h"
 #include "GlobalFunction.h"
 void setup() {
 InitDevice();
 }
 void loop()
 { 
 }

GlobalFunction.h

#ifndef GlobalFunction_H
#define GlobalFunction_H
 void InitDevice();
#endif

GlobalFunction.cpp

#include "GlobalFunction.h"
#include "DeviceDefinition.h"
void InitDevice(){
   if(!Device.Init_Task()){
   }
}

DeviceDefinition.h

#ifndef DeviceDefinition_H
#define DeviceDefinition_H
   #include "Arduino.h"
   #define USE_MCP23017        
   #ifdef USE_MCP23017        
         #include "MCP23017.h"
         MCP_23017Class Device;
   #endif
#endif

Soo i try to explain.
There are several ESP32 and they got different Perepherie one for example the MCP23017.
In the DeviceDefinition.h i choose which perepherie the compilier have to use.
I want to have a changeble Instance of “Device.” and this worked until the newest update.
Now: if i put the #include “MCP23017.h” in “DeviceDefinition.h” befor the “#ifdef Use_MCP23017” it works all fine.

platformio.ini

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
build_unflags = -std=gnu++11
build_flags = -std=gnu++17
build_unflags = -fno-rtti
upload_port = COM11
upload_resetmethod = nodemcu
monitor_port = COM11
monitor_speed =115200
;monitor_rts = 0
;monitor_dtr = 0
lib_deps =
  ArduinoJson@6.11.0
 lib_ldf_mode = chain+

Please remove a space before lib_ldf_mode

1 Like

Thanks for reply but the space only happend because of copy and paste and than format the code in the reply message editor.
Strange is the following.

I bind in the “Device” library in the DeviceDefinition.h. (wich is part of the GlobalFunction Folder)
If i choose the Gateway.h wich is befor the GlobalFunction folder than it works, but if i choose the MCP23017.h or PCA9685 than is get the error.

Thanks and a nice weekend.

Hi @bastian0302

Did you find a solution for this?

I have more or less the same issue:
Want to use Platform-IO with the WOKWI-plugin.

Right now, the simulation uses an I2C-Touch-Input,
my real Hardware uses an SPI-Touch-Input

Both use TS_Point-Classes, so everything should be great including the one or the other… inside the Code the Preprocessor works.
outside (global) the Preprocessor does not work for includes

#define WOKWI

// Touchscreen
#ifdef WOKWI
#include <Adafruit_FT6206.h>
#else
#include “XPT2046_Touchscreen.h”
#endif

I get a lot of Linking-Issues …
Best described, if I just include both

.pio/libdeps/esp32-s3-devkitc-1/XPT2046_Touchscreen/XPT2046_Touchscreen.h:33:7: error: redefinition of ‘class TS_Point’
Compiling .pio\build\esp32-s3-devkitc-1\libc55\FS\FS.cpp.o
33 | class TS_Point {
| ^~~~~~~~
In file included from src/_Main.cpp:50:
.pio/libdeps/esp32-s3-devkitc-1/Adafruit FT6206 Library/Adafruit_FT6206.h:43:7: note: previous definition of ‘class TS_Point’
43 | class TS_Point {
| ^~~~~~~~
Compiling .pio\build\esp32-s3-devkitc-1\libc55\FS\vfs_api.cpp.o
*** [.pio\build\esp32-s3-devkitc-1\src_Main.cpp.o] Error 1

sure I can fix if by commenting the one or the other, but that is the whole point of preprocessor statements :-/

Hallo robinblood,

i used the platformio.ini, where i created 2 Devices (not related to my original request)
and there is a keyname " lib_ignore = DTS726D "
But for this you have to create a library.json
{ "name": "DTS726D", "version": "1.0.0", "description": "3-Phase Energy Meter ", "keywords": "CNC, Energy Meter", "authors": [ { "name": "xxxx" } ] }
So you create the libraries and every on gets such a library.json.
And the devicedefinition in the platformio.ini exlude the othere library.