Header files not found inside subfolders under /src

Hello,

I have a library with all the source file inside /src and there are header files in subfolders. Problem is, the IDE doesn't find the header files when compiling, unless they are directly under /src. I'm using IDE 2.2.1 and I have a library.properties file inside the library root folder. As far as I know, all requirements are fulfilled for a recursive layout, but it still doesn't work.

Arduino/libraries/library_root:
|_ library.properties
|_ src
    |_ file1.h
        |_ subfolder1
            |_subfolder2
                |_file2.h

There's no problem with file1.h, but file2.h is not found, when copiling.

Hi @istvanren. It is mandatory to have an #include directive for a header file in the root of the library's src folder first in code that has a dependency on the library. The reason is that only the root folder is used by the "library discovery" process that adds the library's source folder to the compiler's "search path".

After that, you can add #include directives for paths relative to the root of the src folder.

So you can do this:

#include <file1.h>
#include <subfolder1/subfolder2/file2.h>

But not this:

#include <subfolder1/subfolder2/file2.h>
#include <file1.h>

or this:

#include <subfolder1/subfolder2/file2.h>

If you give it a try and encounter any problems, provide us with a simple demonstration sketch and we'll investigate further.


There is no such requirement for #include directives inside the code of the library itself. For example, you can do this in file1.h:

#include "subfolder1/subfolder2/file2.h"

Follow my three mentioned steps to resolve your problem:

1- Verify the Include Paths:
Ensure that in your source files (e.g., file1.cpp and file2.cpp), you are correctly including the header files using the relative path from the source file's location to the header file. For example:

// In file1.cpp
#include "file1.h"

// In file2.cpp
#include "subfolder1/subfolder2/file2.h"

2- Check library.properties:
In your library.properties file, make sure that you list all the header files along with their relative paths to the /src folder. For example:

src/file1.h
src/subfolder1/subfolder2/file2.h

3- Refresh the IDE:
After making these changes, refresh your Arduino IDE to ensure that it recognizes the updated file structure.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.