Is it possible to search for headers recursively in a specific directory?


I am trying to build an environment suitable to execute Unit Tests (particularly using Google Tests) with some fake headers. The problem I have is that if the headers used in my code are not on the very first level in /lib/SOME_LIBRARY (e.g. lib/SOME_LIBRARY/core/WString.h), the headers are not found and the compiling process fails.

I tried to include the folder I would like to search in with a wildcard, but that does not work (this is another topic that I were not able to add that build flag using a relative path, only using absolute, otherwise the path were always wrong).

Here is my env:

platform = native
lib_compat_mode = off
; test_build_project_src = true
build_type = debug
lib_deps =
	google/googletest @ ^1.10.0
build_flags = 

Is there any way to tell the compiler (directly using flags, or indirectly using some platformio.ini directives) to search for the headers recursively in any specific directory and its sub-directories?

If you have a library folder like

it should have a library.json which declares its needed include flags.

These paths have to be added 1-by-1 in the manifest.

A way to work around this would be by using Advanced scripting and manipulating the CPPPATH environment variable with an array that contains the needed paths – the time needed to write this logic in Python and verify it would be much longer than the time needed to just write it in the library.json thouugh I think. (libraries can also have a extra_script directive)

Per c - How can I use gcc's -I command to add recursive folders - Stack Overflow this only works on clang as the compiler (with a caveat regarding shell-escaping / globbing), so it’s not realy universally usable.

Ah ok, that is then probably why I have the exact same problem using this library:
GitHub - FabioBatSilva/ArduinoFake: Arduino mocking made easy

Because its [library.json]( does not have such includes.

I will try adding them and let you know. Thank you for your help!

BR & LG :slight_smile:

Yes because it has all files in the src/ directory (ArduinoFake/src at master · FabioBatSilva/ArduinoFake · GitHub) and is referencing the header files from the subfolders with their full folder path.

That is all standard and doesn’t need modification of the include flags.

Your library on the other hand

has a non-standard library structure with header files in core.

Also, if there are source files in there which you need to compile, these have to be moved to a src/ folder or the srcDir has to be adjusted.

Also refer

I think you misunderstood my statement about the ArduinoFake library (or I misunderstand you?).

What I meant here, it did NOT also work for me. Particularly: I included the WString.h Arduino file in different header files in my code:

#include <WString.h>

and of course this works normally when I compile for my ESP board, but for my unit tests (in Native env), I tried using the ArduinoFake library, which does not work for my case, probably -if I understand correctly- because the WString.h is not in the standard src folder, therefore I always get the error that the WString.h header file cannot be found.

The Arduino-Fake library auto-includes WString.h through the Arduino.hArduinoFake.harduino/Arduino.h path.

If you need to reference those files directly then either access them via arduino/WString.h or just #include <Arduino.h>, in the case of the Arduino-Fake library. You might also choose to add the folder where WString.h is directly to the include path.

That is exactly the problem, I do not want to change the whole includes to use arduino/WString.h, and I do not want to expose the whole Arduino.h in my files. Therefore I went with the last solution (to add the folder of WString.h to my build_flags).

The only problem I have with the following is that I was not able to pass a relative path to the compiler. For example:

build_flags = 

When checking the verbose output of the compilation, I see that it is always translated into the following:


Which is of course not the path of my project.

Only passing the absolute full path gets translated to the correct relative path, so this:

build_flags = 

gets translated into this:


Trying to access the default variables did not work because they were always empty here in this context for some reason I do not know.

Do you have any suggestion to be able to use a relative path inside my project here without needing to pass the whole absolute path?

Well I got it working using quotation marks in the build flag.

1 Like