Keeping all libraries in the Arduino sketch directory

44,282

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

I just had this same problem (I also like to keep the code self-contained), so I'll just jot down some notes; say I have a MyPdeSketch.pde using MyLibClass.cpp; then I have it organized like this

/path/to/skdir/MyPdeSketch/MyPdeSketch.pde
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.h

(In principle, /path/to/skdir/ here is equivalent to ~/sketchbook/)

 

What worked for me is something like:

mkdir /path/to/arduino-0022/libraries/MyLibClass
ln -s /path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.* /path/to/arduino-0022/libraries/MyLibClass/

After restart of the IDE, MyLibClass should show under ''Sketch/Import Library''.

Note that the only way I can see so far for a library class file to refer to other library files is to include them relatively (from 'current location'), assuming they are all in the same main arduino-0022/libraries folder (possibly related Stack Overflow question: Is it possible to include a library from another library using the Arduino IDE?).

Otherwise, it should also be possible to symlink the MyLibClass directory directly into arduino-0022/libraries (instead of manually making a directory, and then symlinking the files). For the same reason, symlinking to the alternate location ~/sketchbook/libraries could also be problematic.

Finally, a possibly better organization could be:

/path/to/skdir/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyLibClass/MyLibClass.h
/path/to/skdir/MyLibClass/MyPdeSketch/MyPdeSketch.pde

... which, after symlinking to libraries, would force MyPdeSketch to show under the examples for the MyLibClass library in Arduino IDE (however, it may not be applicable if you want to self-contain multiple class folders under a single directory).

EDIT: or just use a Makefile - which would work directly with avr-gcc, bypassing the Arduino IDE (in which case, the sketchbook file organization can be somewhat loosened)..

Share:
44,282

Related videos on Youtube

Miguel Ventura
Author by

Miguel Ventura

Updated on August 10, 2021

Comments

  • Miguel Ventura
    Miguel Ventura almost 3 years

    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.

  • tbraun89
    tbraun89 about 12 years
    If you have them in the same folder you can include them like #include "libName.h". But it seams you have to reload the IDE before you can compile the code.
  • sitilge
    sitilge over 5 years
    @LeandroTupone that sounds wrong but apparently does the thing.
  • Russell Keane
    Russell Keane over 5 years
    Works in 1.8.7.
  • amenthes
    amenthes over 5 years
    did anyone get this pattern to work with libraries that include each other? I am using ESPAsyncWiFiManager for ESP32, and it depends on another library ESPAsyncWebServer. It won't find it, and I would prefer not to alter the third party code files.
  • MV.
    MV. over 3 years
    For libraries that need to include other libraries, just use a relative path in the #include, like "../ESPAsyncWebServer/ESPAsyncWebServer.h" or similar. But this require that you modify the path in the first library.