How to maintain a folder structure for custom libraries ?

I have a custom made sensor library that I intend to organize into a folder structure. For example:
SensorLib/src/FolderA/A.h --> First header file
SensorLib/src/FolderB/B.h --> Second header file
SensorLib/Main.h --> Includes A.h and B.h
SensorLib/Main.cpp --> Includes Main.h
SensorLib/examples /Ex.ino --> Example sketch including Main.h

While trying to do so, the example sketch(.ino file) is not able to locate any of these header files. If I put all of the files together in SensorLib, the code gets compiled.

Error message: fatal error: : No such file or directory

With various sensor library examples already present, I understand that it is a doable job. Any help on this would be really appreciable. Happy to provide more details if required.

Check this:
https://arduino.github.io/arduino-cli/latest/library-specification/#layout-of-folders-and-files
Siome more detailed information about the difference between the three library layout styles is here:

If anything is not clear in that information please let me know.

If it's still not working after you apply that knowledge, please post your library (or a minimal demo library if you prefer) here and I'll help you out. If you click the "Reply" button here, you'll see an "Attachments and other options" link that will allow you to attach a .zip file of your library.

Thanks for sharing the links.

With the documentation, I understand that I would require recursive type of library layout (Arduino version in use: 1.8.13). I have pretty much followed the mentioned points while structuring my library but still could not manage to get the required result. I am attaching the sample library that I am trying to work with. My original sensor library resembles the same folder structure just that number of folder with /src would be much more.

Since the sensor library was huge and difficult to structure in one go, I started trying with smaller chunk. The folder attached does not include the sensor code but is the broken down version of it and hence consists of 3 files only with simple function definition and declaration.

Folder structure explanation for attached lib:
Test_Library/main.h --> Calls <Func.h>
Test_Library/src/core/Func.h --> Contains function prototype for add()
Test_Library/src/core/Func.cpp --> Contains the function definition for add() and includes Func.h

Error remains still the same : fatal error: Func.h: No such file or directory

Am I missing out on any important step? Any help to proceed ahead is welcomed.

Thanks in advance!

Test_Library.zip (1.61 KB)

Did you try it with?

#incluide "src/core/Func.h"

Thanks! This worked with the test library. Will try with sensor library now.

But just to make sure my understanding is correct, I thought "#include <File_name.h>" means there will be a global search for particular header file while "#include "File_name.h"" means it would look for the file in which the current file is located.
Reference link: Different syntax for #include - Development - Arduino Forum

Is it so?

Not sure about the Arduino IDE but regular IDEs, you have to specify the full path with respect to where you are, if the folder is not part of the "include" path is your compiler settings

And I think the <> indicates the file is in the "include" path (default for most compilers), while "" is not so must include the path

OK, I took a look and there are several problems:

The metadata file must be named exactly "library.properties", but your metadata file is named "libraries.properties". See: Redirecting

Recursive layout libraries should have all source and header files under the src subfolder, but you have a header file in the root of the library folder. See: Redirecting

Next, it's necessary for you to understand how Arduino's library discovery system works. You can learn all about that here:
https://arduino.github.io/arduino-cli/latest/sketch-build-process/#dependency-resolution

That's a pretty dense read, so I'll give some important points:

  • If the double quotes syntax is used, the relative or absolute path is checked.
  • If found at that path only the specified header file is included. The accompanying source files in that folder are not given any special treatment.
  • If not found at that path or in the current include search paths list, the root of the source folder of all libraries installed in the standard Arduino library folder installation locations (e.g., /libraries, /libraries, /libraries, core library, core variant library) is searched for the file.
  • If the file is found, that library folder is added to the include search paths list and the source files of that library are compiled.

So let's start with the correct structure of the library:

libraries
|_ Test_Library
|_ library.properties
|_ examples
| |_ etc.
|_ src
|_ Test_Library.h
|_ core
|_ Func.h
|_ Func.cpp

You'll notice I renamed your "main.h" to "Test_Library.h". This is not absolutely necessary, but a super generic file name like this is highly likely to result in collisions. As I said, all the libraries root source folders are searched for this file. If multiple libraries contain a file of that name, the build system will try to pick the best one, but there's no guarantee it will get it right. One of the factors used in that determination is whether the filename matches the library root folder name. So best practices is for the primary header file name to match the library folder name. Since Library Manager uses the library.properties name field value as the library installation folder name, it is also best practices for that name to match.

In your main.h you have this:

#include <Func.h>

This will cause the root source folder of all libraries to be searched for a file named "Func.h". But your "Func.h" is in a subfolder, so, as @hzrnbgy explained already, you must provide the subfolder name in the #include directive.

Now, if you wanted to use this library in a sketch, you would add this #include directive:

#include <Test_Library.h>

Thank you @Pert for your efforts! This explanation pretty much clears everything I was stuck with. I am getting started with my sensor library and will make sure to consider the structure you have mentioned. Will come back in case of queries.

You're welcome. I'm glad if I was able to be of assistance. I'm happy to answer any questions you might have. It's a bit confusing at first, but after you've worked with the system a little bit it gets a lot easier.

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