Include header from include dir into lib dir

This is probably a dumb question, but I’m having some difficulties understanding PlatformIO lib directory include path. Here is my setup, I have some global header in the include dir, subfolders in the lib dir with my private libraries, like below:

|--include
|  |- global_header.h
|
|--lib
|  |--Foo
|  |  |- Foo.cpp
|  |  |- Foo.h
|
|- platformio.ini
|--src
   |- main.cpp

I want to be able to #include "global_header.h" in both Foo.h and main.cpp. If I just try to compile without changing anything in my platformio.ini, I get the following error:

lib/Foo/Foo.h:6:10: fatal error: global_header.h: No such file or directory

According to this answer, this is the intended behavior, which ok. So I tried to add build_flags = -Iinclude to my platformio.ini, but then I got an unexpected error:

include/global_header.h:5:10: fatal error: ArduinoLog.h: No such file or directory

But ArduinoLog is not a private library, I have lib_deps = thijse/ArduinoLog in my platformio.ini, and it has been working fine until now.

What would be the best approach to solve this? Do I move the global_header file somewhere else? Add another build_flag? I don’t mind rearranging some things if I’m able to use the header file like I stated in the bolded text above.

Duplicate of PIO library doesn't see header files in project's include folder - #8 by valeros.

Can also be solved by giving lib/Foo a library.json manifest that adds the project’s include (and optionally src/) folder to include paths of the library. Example. Then no modification in the user’s platformio.ini has to be made.

I had tried to add a library.json, but wasn’t using the correct options, so thanks for the example!

It still didn’t solve the problem with ArduinoLog though, I wanted to include the ArduinoLog library through the global header, so I don’t need to include it in every private library, aka:

In global_header.h

#include <ArduinoLog.h>

In lib/Foo.h

#include "global_header.h"
// I would assume I don't need this include here
//#include <ArduinoLog.h>

But I only managed to compile the project by doing exactly that, including ArduinoLog in Foo.h.

This probably has to do with the library dependency finder no seeing the include in its scan chain, try in the library.json, try all values of libLDFMode.

It turns out apparently I had two problems, one was about include the global header file, and the other was the library dependency finder configuration. By adding both lines to my platformio.ini everything compiled nicely:

build_flags = -Iinclude
lib_ldf_mode = chain+

I found this solution better than setting this configurations in a library.json file because my project is going to have several private libs, so instead of having this duplicated in every private lib, I’d rather have one global configuration.

1 Like