Specific libraries & directories organization

I would like to find some script about how to save specific libraries with each sketch and others directory organization rules. Does anybody know some link to that kind of tutorials ? Thanks

Some information here:
https://arduino.github.io/arduino-cli/latest/sketch-specification/#src-subfolder

In general, if you want to use a library specifically for a sketch then put the library files in the same folder as the sketch and include it like this

#include "library.h"

This will force the compiler to look for the library in the sketch folder in the first instance and if it is not found then it will look in the libraries folder of your sketchbook folder

If you use

#include <library.h>

then the compiler will look immediately for the library in the libraries folder of your sketchbook folder

What problem are you trying to solve ?

pert:
Some information here:
Redirecting

Thank you pert, that information is absolutely usefull. Exectly what I am loooking for...

UKHeliBob:
In general, if you want to use a library specifically for a sketch then put the library files in the same folder as the sketch and include it like this

#include "library.h"

This will force the compiler to look for the library in the sketch folder in the first instance and if it is not found then it will look in the libraries folder of your sketchbook folder

If you use

#include <library.h>

then the compiler will look immediately for the library in the libraries folder of your sketchbook folder

What problem are you trying to solve ?

Thanks for help. I did not know about the difference between include with "" and <>.

You help plus the article mentioned in answer #1 above, answered everything I want: Redirecting

:slight_smile:

UKHeliBob:
In general, if you want to use a library specifically for a sketch then put the library files in the same folder as the sketch and include it like this

#include "library.h"

This will force the compiler to look for the library in the sketch folder in the first instance and if it is not found then it will look in the libraries folder of your sketchbook folder

If you use

#include <library.h>

then the compiler will look immediately for the library in the libraries folder of your sketchbook folder

What problem are you trying to solve ?

UKHeliBob, Unfortunatelly your valuable clue did not work yet.
Suppose I have the following directory structure:

/ prog
| prog.ino
/ src
| lib.h
| lib.cpp

In prog.ino I have: #include "lib.h" expecting to have used the lib.h under /prog/src directory

In this situation the Arduino IDE is always using the lib.h from libraries on sketchbook not my one from /prog/src

I have also tryied:

/ prog
| prog.ino
/ lib
| lib.h
| lib.cpp

and also:

/ prog
| prog.ino
/ lib
/src
| lib.h
| lib.cpp

All resulting the same: The lib.h used is always from libraries on sketchbook...

Any idea about where is the mistake ? Thanks

if your library files are in a folder named src which is in the sketch folder then use

#include "src/lib.h"

Using <> round the library name will always force the compiler to look for the library in a central location. Personally I prefer to work with the .h and .cpp files in the sketch directory when using local library files as that way they show up as tabs in the IDE and can be edited there

AntonioTesta:
I have also tryied:

/ prog
| prog.ino
/ lib
| lib.h
| lib.cpp

and also:

/ prog
| prog.ino
/ lib
/src
| lib.h
| lib.cpp

@UKHeliBob has already explained how to adjust your #include directives to work with the src subfolder, so you should be good to go with that approach.

As for this approach of naming the folder "lib", you should be aware that the "src" subfolder has special treatment. This subfolder, and only this subfolder is compiled recursively by the Arduino development software. If you use any other folder name the contents will not be compiled. Because it's recursive, you are welcome to have any folder structure you like under the src subfolder (just make sure to adjust your #include directives accordingly). This can be very useful if you have multiple libraries you want to bundle with the sketch, or if the bundled library has a nested folder structure. The approach of putting the library in the sketch's root folder works fine with one or two simple libraries, but not so much with the more complex libraries.

A couple major caveats about the src folder approach you should be aware of:

  • The classic Arduino IDE's File > Save As... has a bug where the src folder is not saved. File > Save works fine though. This bug has been fixed in Arduino IDE 2.0.0.
  • Arduino Web Editor does not have support for the src subfolder.

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