Missing include file when library include file not included

I have a library, MQTTRemote, that depends on WiFi.h for ESP32 and ESP8266WiFi.h for ESP8266.

If I add a dependency on this library and compile the project, the compilation is successful.


platform = espressif8266@4.0.1
board = d1_mini
framework = arduino
lib_deps =


#include <Arduino.h>
// If <MQTTRemote.h> is included, the project compiles successfully.
// If <MQTTRemote.h> is not included, compilation failure in MQTTRemote.h on missing ESP8266WiFi.h
#include <MQTTRemote.h>

void setup() { Serial.begin(115200); }

void loop() {}

However, if don’t include the library header file, <MQTTRemote.h> in main.cpp, then the project doesn’t compile with a compilation error in the library itself:

In file included from .pio\libdeps\d1_mini\MQTTRemote\src\MQTTRemote.cpp:1:
.pio\libdeps\d1_mini\MQTTRemote\src\MQTTRemote.h:12:10: fatal error: ESP8266WiFi.h: No such file or directory

How come the library fail to compile when the header file is not included, but compiles as soon as it is?

I experience the same when compiling for ESP32 (but then on missing WiFi.h).

Yes, if I’m not using the library I don’t need to depend on it, but at this point I’m trying to understand what is wrong and if there is anything I can do to still depend on the library but (not yet) using it, or if the library itself is wrong.


Because it’s a PlatformIO quirk that by default libraries that are in your lib_deps but are not included in the code fail to have their dependencies discovered correctly.

Changing lib_ldf_mode might help.

Setting lib_ldf_mode to deep “solves” the problem. Thanks :).

Not sure that I’m super happy with the solution, as it is not clear for the library user to know that they might be needing to do this (in case the library in question contain several h/cpp files that includes different parts from the framework). But I assume this is by design given that the library finding mode is configurable.