Keeping all libraries in the Arduino sketch directory

I know that you are supposed to place any external libraries under the "libraries" folder of the arduino install directory, but I have a project that uses several libraries that I have created for the project and mainly to keep all that code self contained and out of the main pde file. However, I have tried to place the libraries in the same directory as the main PDE file so that I can more easily keep everything synced up in subversion (I work on this on multiple computers) and I don't want to have to keep going back and syncing up the libraries separately. Also, just for the sake of being able to easily zip of the sketch folder and know that it contains everything it needs.

I've tried adding the header files to the sketch as a new tab, but that doesn't seem to work at all... don't even care if they should up in the arduino IDE.

I've also tried adding the libraries to the sketch directory in subdirectories (what I would greatly prefer) and then linking to them as:

#include "mylib/mylib.h"

and

#include <mylib/mylib.h>

But both of these result in file not found errors.

Is this possible? And, if so, how do I include them in the main file for building? Preferably in their own subdirectories.


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.h

all: $(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.


Unfortunately the Arduino IDE is awful and shows no signs of improving. There is no real build system so it only lets you build programs that reside in a single directory.

The only real solution is to write a makefile, then you can use a real IDE. I'm hopeful that one day someone will write an Arduino plugin for QtCreator.

Here's an example makefile:

http://volker.top.geek.nz/arduino/Makefile-Arduino-v1.8