Build fails because missing library include (Arduino framework)

Hi,

I was trying to migrating from Arduino to PlatformIO, but I’ve got a problem. “ESP Logger” library fails during compiling. I’ve just tried to compiled the simplest example included in this library.

It fails in include process. This library makes use of SD and SPIFFS libraries embedded in ESP32 Arduino core to implement the “log” functionalities. The first example use only the SPIFFS implementation, but of course, the toolchain will compile all the *.cpp in the library folder, including the one regarding the SD implementation. Unfortunately, the latter fails during compilation saying:

fatal error: SD.h: No such file or directory

I’ve found a workaround reading this post, basically adding an include of SD.h in the main file of the project.

Is this the proper solution or is there a proper way to include the Arduino “core” libraries? Because it seems weird to include unnecessary files in main file, especially considering that in Arduino this library works fine.

Thanks for your work.

1 Like

That is the simplest way, but depending on how a library is written, not the only way. Since the Dependency Graph at the top of the build doesn’t acknowledge that the SD library is needed…

image

… but it clearly is as the build will fail. I had a look at the line mentioned in the error (30) …

image

… so what is probably happening is there that because the examples uses SPIFFS, #include <SD.h> isn’t been done in a file that is included in the nested includes, but since all files will be processed in the build, that then causes the SD library to not be found when logger_sd.cpp is processed. Probably the better way to fix this is telling the Library Dependency Finder to process all the C/CPP files for dependencies, and since preprocessor macros aren’t used, deep is sufficient. i.e.

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps = ESP Logger
lib_ldf_mode = deep

image

Thank you for the clear response. It works.

However, I was wondering if my method of handling “include” directives is wrong or, in general, it is a bad practice. Usually, I try to import only the strictly required file.

1 Like

I wouldn’t recommend it (#include <SD.h>) as the workaround/fix for the error, because for starters, the SD library isn’t actually used, so including it is just confusing. Plus, as you say, only import the required files. Better to tell PlatformIO to dig a little deeper and figure it out itself. :wink:

Ok, it seems good. As you are talking about “starter”/“beginners”, can I specify

lib_ldf_mode = deep

only for my library?

So starters shouldn’t bother to modify lib_ldf_mode = deep in their platform.ini file

1 Like

There is a libLDFmode flag the the library.json spec which looks like it should do exactly that…

1 Like

Thanks, It is exactly I was searching for! I didn’t see it while reading the docs…

1 Like