Strange error with #define #if #include combo


#define LOG_TYPE_NVF                1

#if LOG_TYPE_NVF
  #include <fstream>
#endif //LOG_TYPE_NVF
#if 1
  #include <LittleFS.h>
#endif //LOG_TYPE_NVF

Works fine

#define LOG_TYPE_NVF                1

#if LOG_TYPE_NVF
  #include <fstream>
#endif //LOG_TYPE_NVF
#if LOG_TYPE_NVF
  #include <LittleFS.h>
#endif

Fails to compile with

C:/Users/rosen/OneDrive/Documents/PlatformIO/Libraries/DMR_Framework/src/DMR_Framework.h:124:12: fatal error: LittleFS.h: No such file or directory

******************************************************************
* Looking for LittleFS.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:LittleFS.h"
* Web  > https://registry.platformio.org/search?q=header:LittleFS.h
*
******************************************************************

   #include <LittleFS.h>
            ^~~~~~~~~~~~
compilation terminated.
*** [.pio\build\esp32dev\lib3ce\DMR_Framework\DMR_Framework.cpp.o] Error 1

I have several other includes that are controlled in the same way that work fine. The json file has the line
“C:/Users/rosen/.platformio/packages/framework-arduinoespressif32/libraries/LittleFS/src”,
so I’m not sure why the failure.

Has anyone ever seen this before?

Works also fine for me.

What’s the content of your platformio.ini?

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
upload_protocol = espota
upload_port = 192.168.0.xxx
upload_flags = 
	--port=3232
	--auth=xxx
build_flags = 
	-I include/
	-I lib/LCDM/
lib_ldf_mode = chain+
lib_deps = 
	WiFi
	ESPmDNS
	Update
	ArduinoOTA
	Wire
	FS
	LittleFS
	Preferences
	ESP Telnet
	SPI
	marcoschwartz/LiquidCrystal_I2C@^1.1.4
	milesburton/DallasTemperature@^3.11.0
	gbertaz/NonBlockingDallas@^1.1.0
	bblanchon/ArduinoJson@^7.2.1
	adafruit/Adafruit BusIO @^1.16.2
	adafruit/Adafruit GFX Library@^1.11.11
	adafruit/Adafruit SSD1306@^2.5.13
	hafidh/FlowSensor@^1.5.0
	wollewald/ADS1115_WE@^1.5.3
	paulstoffregen/OneWire@^2.3.8
	jomelo/LCDMenuLib2@^2.2.8
	me-no-dev/ESP Async WebServer@^1.2.4
	lennarthennigs/ESP Telnet@^2.2.2
	adafruit/Adafruit ADS1X15@^2.5.0
	knolleary/PubSubClient@^2.8
	DMR_Framework=symlink://C:/Users/rosen/OneDrive/Documents/PlatformIO/Libraries/DMR_Framework
	
monitor_speed = 115200

[platformio]
description = JS Demo import

But using a DEFINED variable as opposed to a 1 shouldn’t change where it looks for files.

It also fails if I use “true” instead of “1”. And it has done it on other includes before and I was able to change the order and get it to fail on different ones before.

My best guess is that the preprocessor is hitting some sort of limit on the file while it is doing the replacements.

The other strange thing is that the Dependency graph is flat.

Dependency Graph
|-- WiFi @ 2.0.0
|-- ESPmDNS @ 2.0.0
|-- Update @ 2.0.0
|-- ArduinoOTA @ 2.0.0
|-- Wire @ 2.0.0
|-- FS @ 2.0.0
|-- LittleFS @ 2.0.0
|-- Preferences @ 2.0.0
|-- ESP Telnet @ 2.2.2
|-- SPI @ 2.0.0
|-- LiquidCrystal_I2C @ 1.1.4
|-- DallasTemperature @ 3.11.0
|-- NonBlockingDallas @ 1.1.0
|-- ArduinoJson @ 7.2.1
|-- Adafruit BusIO @ 1.16.2
|-- Adafruit GFX Library @ 1.11.11
|-- Adafruit SSD1306 @ 2.5.13
|-- FlowSensor @ 1.5.0
|-- ADS1115_WE @ 1.5.3
|-- OneWire @ 2.3.8
|-- LCDMenuLib2 @ 2.2.8
|-- ESP Async WebServer @ 1.2.4
|-- Adafruit ADS1X15 @ 2.5.0
|-- PubSubClient @ 2.8.0
|-- DMR Framework @ 0.1.1
|-- LCDM
|-- AsyncTCP @ 1.1.1
Building in release mode
Compiling .pio\build\esp32dev\lib3ce\DMR_Framework\DMR_Framework.cpp.o
Archiving .pio\build\esp32dev\lib3ce\libDMR_Framework.a
Linking .pio\build\esp32dev\firmware.elf
Retrieving maximum program size .pio\build\esp32dev\firmware.elf
Checking size .pio\build\esp32dev\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [==        ]  17.1% (used 56108 bytes from 327680 bytes)
Flash: [========  ]  82.5% (used 1081073 bytes from 1310720 bytes)
Building .pio\build\esp32dev\firmware.bin
esptool.py v4.5.1
Creating esp32 image...
Merged 27 ELF sections
Successfully created esp32 image.
========================================= [SUCCESS] Took 74.23 seconds ========================================= *  Terminal will be reused by tasks, press any key to close it.

It was showing multiple levels at one point but now it isn’t. Is there a way to increase the verbosity of the build process? So I can maybe see where it is/isn’t looking for the include files.

I’m pretty sure the issue comes from the DMR_Framework (or it’s location)
Please move this into your lib folder and remove the entry from the lib_deps

OneDrive is a well known troublemaker.

All these are builtin libraries and are available by the framework. Remove them from your lib_deps.

3232 is the default port and can be removed.

What is your your intention with these settings?

I already tried pausing OneDrive to see if it was causing the issue. NO change.

As part of my troubleshooting I had changed the lib_ldf_mode = chain+ to lib_ldf_mode = off and manually added all of the libraries I used to see if that was the problem. Didn’t fix the problem and I just left them in there.

The framework is shared between several projects and I’m still adding features so the symlink is the best option for adding it. The header it can’t find is one of the builtin libraries not part of the framework so I’m not following on how it would be only that one header that can’t be found based on how the #if is written.

The intention is to have the include and lib/LCDM folders scanned for files. The include folder has my .h files and the LCDM folder is all of the LCD Menu Library support/configuration files.

It’s not about pausing the serivce.

You can remove the libraries for the reason i mentioned before.

Please move the DMR_Framework out of the one drive location as i asekd you to do. Move it into the lib folder to the current project. Just for testing! If this will fix the issue, we can think about a permanent solution.

From the documentation:

-Idir
Add the directory dir to the list of directories to be searched for header files.

This already applies to the include and lib lib directories - even without the build_flags!.

I created a a new directory “C:\Projects” so it’s out of the OneDrive control. Copied my project, Copied the DMR Framework into the Lib folder removed the SymLink.

The project compiled (without the problem line). SO I confirmed that everything copied over correctly. I then changed the #if 1 (that works) into #if LOG_TYPE_NVF and it fails with the same error. Also tried #if true and it still fails.

In file included from include/main.h:12,
                 from src/main.cpp:15:
lib/DMR_Framework/src/DMR_Framework.h:111:12: fatal error: LittleFS.h: No such file or directory

I tried removing the -Idir and then it failed with other issues. It couldn’t find anything in the /include folder so (as I had read in several other threads) I added the build_flags = -I include/ back in and was again able to compile with the #if 1 but not any other #if .

So bottom line is I’m right where I was before with not being able to use a #defined value in the #if for LittleFS.h but I am using them for other headers without issue.

I’ll ask again is there any flag or setting to show the search paths that it tries on each #include to resolve them?

You can do a verbose build:
PIO Icon → PROJECT TASKS → [envname] → Advanced → Verbose Build

Unfortunately, I cannot find the DMR_Framework library you referenced.

Can you provide a complete test project to reproduce the error?

The DMR_Framework is the framework that I am writing. It’s not published anywhere. I’ll try to create a new sample project that generates the error.

I would upload a zip of it but I only see where to upload images not zip files. Maybe my account is too new.

A github repo would be fine :slight_smile:

If I did it correctly you have been added to the repository.

The line in question is in DMR_Framework.h lines 120-121.

The project is currently set to have almost all options turned off and I still get the error.

1 Like

Well I changed a #define value and now I can no longer compile. My laptop now won’t recognize anything in the libs folder.

If I add the lib folder to the build_flags it will “see” the files in intellisense and compile up until linking then fail that the referenced functions aren’t found. The .cpp files are never referenced in the build log.

If I move them to the project src folder it all compiles just fine.

I have tried removing VS Code and Platformio and reinstalling but still not getting it to recognize anything in the libs folder. I have it setup like the readme but unless I add the build_flags the c_cpp_properties.json doesn’t list the libs folder. Not sure if it should be, but I would think it should be listed somewhere. Is there somewhere else to look to see if it’s not being listed correctly or why it is no longer processing that folder.

Can you provide a minimal example that reproduces the error?

I just tried to create a new project to replicate it and the new project doesn’t have the libs folder nor the read me that describes the folder layout isn’t there.

Was there a change or does it only create it on imported Arduino projects?
image

PS C:\Platformio\Project\libTest> pio upgrade
You’re up-to-date!
PlatformIO 6.1.16 is currently the newest version available.
PS C:\Platformio\Project\libTest>

No that is not normal. Something strange is going on your computer…

What about running the terminal command
pio project init -b esp32dev --ide vscode ?

Your PATH must include C:\Users\<username>\.platformio\penv\Scripts

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

libTest.h

// put function declarations here:
extern int myFunction(int, int);

libTest.cpp

// put function definitions here:
int myFunction(int x, int y) {
  return x + y;
}

main.cpp

#include "main.h"
#include "libTest.h"



void setup() {
  // put your setup code here, to run once:
  int result = myFunction(2, 3);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Build Log:

Executing task: C:\Users\rosen\.platformio\penv\Scripts\platformio.exe run 

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
--------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (6.9.0) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, 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.20017.241212+sha.dcc1105b
 - tool-esptoolpy @ 1.40501.0 (4.5.1)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch5
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 35 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Building .pio\build\esp32dev\bootloader.bin
Generating partitions .pio\build\esp32dev\partitions.bin
esptool.py v4.5.1
Creating esp32 image...
Merged 1 ELF section
Successfully created esp32 image.
Compiling .pio\build\esp32dev\FrameworkArduino\Esp.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\FirmwareMSC.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\FunctionalInterrupt.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\HWCDC.cpp.o
Compiling .pio\build\esp32dev\FrameworkArduino\HardwareSerial.cpp.o
src/main.cpp:2:10: fatal error: libTest.h: No such file or directory

*****************************************************************
* Looking for libTest.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:libTest.h"
* Web  > https://registry.platformio.org/search?q=header:libTest.h
*
*****************************************************************

 #include "libTest.h"
          ^~~~~~~~~~~
compilation terminated.
*** [.pio\build\esp32dev\src\main.cpp.o] Error 1
================================== [FAILED] Took 12.68 seconds ==================================

 *  The terminal process "C:\Users\rosen\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

If I move the files into the “include” and “src” folders from the “libs” then it compiles correctly.

First part of the c_cpp_properties.json has the “include” and “src” but not the “libs”.

    "configurations": [
        {
            "name": "PlatformIO",
            "includePath": [
                "c:/Platformio/Project/IssueTest/include",
                "c:/Platformio/Project/IssueTest/src",
                "C:/Users/rosen/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/newlib/platform_include",
                "C:/Users/rosen/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include",
                "C:/Users/rosen/.platformio/packages/framework-arduinoespressif32/tools/sdk/esp32/include/freertos/include/esp_additions/freertos",
...

For libraries located in the lib folder you have to use the global include:
#include <libTest.h>

The same applies to libraries which are installed via the lib_deps in platformio.ini

There is no need to declare the function as extern because C / C++ have external linkage by default. Change this to

// put function declarations here:
int myFunction(int, int);

Platformio has bugs… Whenever possible only use LDF mode chain
All libs should have a library.json. Without strange issue can happen.
Avoid symlink, can makes issues. Never again never use any cloud drive for the source. Keep the source filepath as short as possible. Best is C:<your_project> If path is too long you run in Windows path length limit! (Weird issues about file not found and other unrelated error messages). Do you a favor and install WSL The path length limit is gone and compile will be faster.