[SOLVED] Libraries including each other lead to 'no such file or directory error'

Hi,
I am having a project where multiple functionalities each have their own library. The library functions of course require each other. However at compiling I get the error lib/timer/timer.h:3:17: fatal error: led.h: No such file or directory. Why can the compiler not see the file? I thought the include guard takes care of preventing a double include or loops, but the file should be still visible.

main.cpp:

#include <Arduino.h>
#include "button.h"
#include "led.h"
#include "web.h"
void setup(){}
void loop(){}

button.h:

#ifndef BUTTON_H
#define BUTTON_H
#include "led.h"
#include "web.h"
class button{};
#endif

led.h:

#ifndef LED_H
#define LED_H
#include "timer.h"
class led{};
#endif

timer.h:

#ifndef TIMER_H
#define TIMER_H
#include "led.h"
#include "web.h"
class timer{};
#endif

web.h:

#ifndef WEB_H
#define WEB_H
#include "timer.h"
class web{};
#endif

The corresponding .cpp files all just contain a #include "[library].h"

I am open for structuring my code differently, I just did not come up with a better idea. Thank you for your help.

Full build log:

Processing nodemcuv2 (platform: espressif8266; board: nodemcuv2; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif8266/nodemcuv2.html
PLATFORM: Espressif 8266 > NodeMCU 1.0 (ESP-12E Module)
HARDWARE: ESP8266 80MHz 80KB RAM (4MB Flash)
Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF MODES: FINDER(chain) COMPATIBILITY(soft)
Collected 55 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ArduinoJson> 5.13.2
|-- <led>

|   |-- <timer>
|   |   |-- <web>
|-- <button>
|   |-- <led>
|   |   |-- <timer>
|   |   |   |-- <web>
|   |-- <web>
|-- <web>
Compiling .pioenvs/nodemcuv2/lib687/web/web.cpp.o
In file included from lib/web/web.h:3:0,
from lib/web/web.cpp:1:
lib/timer/timer.h:3:17: fatal error: led.h: No such file or directory

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

#include "led.h"
^
compilation terminated.
Archiving .pioenvs/nodemcuv2/libc0c/libbutton.a
Archiving .pioenvs/nodemcuv2/libFrameworkArduinoVariant.a
Indexing .pioenvs/nodemcuv2/libc0c/libbutton.a
Indexing .pioenvs/nodemcuv2/libFrameworkArduinoVariant.a
Compiling .pioenvs/nodemcuv2/FrameworkArduino/Esp-version.cpp.o
*** [.pioenvs/nodemcuv2/lib687/web/web.cpp.o] Error 1
================================================================================================== [ERROR] Took 1.01 seconds

This doesn’t look to be a layout issue… but more the default Library Dependency Finder mode… the default chain mode just isn’t figuring it out… if you add lib_ldf_mode = chain+ to your environment in platformio.ini it should all start working again.

I think the root cause is that although chain mode does walk through all the source files and included libraries to look for dependencies, it doesn’t process preprocesssor conditionals… which would mean it doesn’t process include guards properly. chain+ (and deep+) does though. That’s my theory so far anyway :wink:

1 Like

Thank you very much for your quick help. With the chain+ mode almost everything worked.
The only thing that did not work was the WebSockets library, I think because of the #elif (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266) line. It gave an error it could not find ESP8266WiFi.h, which was included inside the #elif statement. Moving it up before the statement works although I am aware it is not a great fix ¯\_(ツ)_/¯

1 Like