Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 647
Adam Haile Avatar asked Jan 16 '11 14:01

Adam Haile


People also ask

Where should Arduino libraries be stored?

Please note: Arduino libraries are managed in three different places: inside the IDE installation folder, inside the core folder and in the libraries folder inside your sketchbook.

Where are Arduino sketches stored?

You can see the sketches in the Sketchbook folder by going to File > Sketchbook. The default name of the Sketchbook folder is “Arduino” and the default location of the Sketchbook folder is in the “My Documents” folder (or just “Documents” for Mac users).

How do I save my Arduino library?

Simply open the Arduino IDE, click on “Sketch” > “Include Library” > “Add . ZIP Library…”, and browse to find your . zip archive. The Arduino IDE will extract the archive, place the library in the Arduino/libraries/folder, and update itself so you don't need to restart it.


3 Answers

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

like image 70
Uwe Hafner Avatar answered Sep 23 '22 03:09

Uwe Hafner


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.

like image 45
KyleWpppd Avatar answered Sep 26 '22 03:09

KyleWpppd


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.

like image 43
Tom Swirly Avatar answered Sep 24 '22 03:09

Tom Swirly