Keeping all libraries in the Arduino sketch directory Keeping all libraries in the Arduino sketch directory c c

Keeping all libraries in the Arduino sketch directory


I had the same issue. Solved it for Arduino IDE > 1.8. Seems a specialty in newer IDEs (?) according to the reference (see bottom link).

You have to add a "src" Subdirectory before creating a library folder. So essentially your project should look like this:

/SketchDir (with *.ino file)  /SketchDir/src  /SketchDir/src/yourLib (with .h and .cpp file)  

and finally in your sketch you reference:

#include "src/yourLib/yourLib.h"  

otherwise in my case - if I am missing the "src" folder - I get the error message that it cannot find the yourLib.cpp file.

Note: I am using a windows system in case it differs and actually VS Code as wrapper for Arduino IDE. But both IDE compile it with this structure.

References:https://forum.arduino.cc/index.php?topic=445230.0


For the sketches I have, the "*.h" and "*.cpp" library files actually reside in the same folder as the sketch, and I call them like "someheader.h". I also noticed that if I go into sketch menu and add file... that the file doesn't appear until I close and reopen the sketch.


I agree with you; this is an intolerable way to develop software: it requires every file that you need to be in the same directory as the main program!

To get around this, I use make to put together a single .h file from my .h and .cpp sources - you can see this used in this Makefile:

PREPROCESS=gcc -E -C -x c -iquote ./src# -E : Stop after preprocessing.# -C : Don't discard comments.# -x c : Treat the file as C code.# -iquote ./src : Use ./src for the non-system include path.TARGETS=sketches/morse/morse.hall: $(TARGETS)clean:    rm $(TARGETS)%.h: %.h.in    $(PREPROCESS) $< -o $@

Arduino is very picky about file endings - if you put a .cpp or .cc file in its directory it automatically uses it in the source, and you can't include anything that's not a .cpp, .cc or .h - so this is about the only way to do it.

I use a similar trick also to put together JavaScript files here.

This requires that you run make after editing your files, but since I'm using an external editor (Emacs) anyway, this is zero hassle for me.