How to create a library with subfolders that are recursively compiled?

Library specification - Arduino CLI is wrongly saying that you can just create library with folder "src" and put everything inside, but that doesn't work.

What I tried:

Documents/Arduino/libraries/my_lib/my_lib.h
Documents/Arduino/libraries/my_lib/my_lib.cpp
Documents/Arduino/libraries/my_lib/src/subfile.h
Documents/Arduino/libraries/my_lib/src/subfile.cpp

In sketch, when I #include <my_lib.h> it seems to work, but compiles only my_lib.cpp and doesn't compile subfile.cpp which results in linker error with undefined references.

Documents/Arduino/libraries/my_lib/src/my_lib.h
Documents/Arduino/libraries/my_lib/src/my_lib.cpp
Documents/Arduino/libraries/my_lib/src/subfile.h
Documents/Arduino/libraries/my_lib/src/subfile.cpp

Doesn't work at all - my_lib.h No such file or directory Arduino won't find it in src

Documents/Arduino/libraries/my_lib/src/my_lib.h
Documents/Arduino/libraries/my_lib/src/my_lib.cpp
Documents/Arduino/libraries/my_lib/src/src/subfile.h
Documents/Arduino/libraries/my_lib/src/src/subfile.cpp

Same problem.

What is the correct layout where I can split the library into many files, while exposing only one entry header file to user and get the compiler to compile all .cpp files, not just the single cpp file with same name as the library?

I am using Arduino IDE 2.0.3

Did you include the subfile.h header anywhere in your source files in the main directory?

EDIT:

subfile is included from my_lib.cpp as it's used by library, it's not meant to be included directly by the user code in sketch.

The problem wasn't with including, that wouldn't even compile. Problem was with linking. In compiler output I could see that Arduino only compiled my_lib.cpp but didn't compile subfile.cpp, resulting in undefined reference.

I need to somehow convince Arduino to compile everything in src folder, which it doesn't, but should according its own documentation.

the root of the library must have library.properties for src folder to work

This is something that should definitely be added to documentation. Right now it say that since some new version this file is also supported and allows to add some metadata etc. but it doesn't say anywhere that this file is MANDATORY for the compiler to work

This file is not mandatory if you put all your files in the root folder of the library.

the doc has

It must be located in the root of the library folder.

ok so it should say it's mandatory if you want to use the new "src" based layout. There is no mention about this requirement in part of documentation that describes that new layout.

For libraries intended to be used with Arduino IDE 1.5.x+ only, the source code resides in the src folder. For example:

Servo/src/Servo.h
Servo/src/Servo.cpp

The source code found in src folder and all its subfolders is compiled and linked in the user’s sketch. Only the src folder is added to the include search path (both when compiling the sketch and the library). When the user imports a library into their sketch (from the Arduino IDE's "Sketch > Include Library" menu or the Arduino Web Editor's "Include" button), the default behavior (configurable via the library.properties includes field) is for an #include statement to be added for all header (.h) files in the src/ directory (but not its subfolders). As a result, these header files form something of a de facto interface to your library; in general, the only header files in the root src/ folder should be those that you want to expose to the user's sketch and plan to maintain compatibility with in future versions of the library. Place internal header files in a subfolder of the src/ folder.

This part would be far less confusing if there was something like "Note: this only applies if library.properties file exists"

Right now there is no indication that it is mandatory part of library

You may wanna try PlatformIO for anything more advanced than "sketches".

it is the 'new' format which uses library.properties and src folder

1.5 library format (rev. 2.2)
The most significant addition to the format is the ability to add information about the library itself through a properties file called library.properties.
This file allows the Library Manager to search and install a library and its dependencies in an easy and automated way. It must be located in the root of the library folder.

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