Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to include a library from another library using the Arduino IDE?

I'm trying to write an Arduino library (effectively a C++ class) which itself references another library I have installed in my Mac's ~/Documents/Arduino/libraries directory.

At the top of the .cpp of the library I'm writing, I've tried

#include <ReferencedLibrary.h>

and

#include "ReferencedLibrary.h"

... neither of which work. I can successfully #include <ReferencedLibrary.h> from sketches in my ~/Documents/Arduino directory. Am I missing something or is this a limitation of the Arduino IDE/makefile? Is there a workaround?

like image 831
Robert Atkins Avatar asked Jun 28 '11 09:06

Robert Atkins


People also ask

How do I import libraries into Arduino IDE?

In the Arduino IDE, navigate to Sketch > Include Library > Add . ZIP Library. At the top of the drop down list, select the option to "Add . ZIP Library''.

How do I add a library to GitHub Arduino IDE?

In this case, I will be getting an SD card read/write library, which can be found on GitHub. First, download the library as a ZIP, which is done by clicking the green “Clone or download” button and then clicking “Download ZIP”. Once downloaded, go to the Arduino IDE and click Sketch > Include Library > Add .

Can you include C++ libraries in Arduino?

To add your own library, create a new directory in the libraries directory with the name of your library. The folder should contain a C or C++ file with your code and a header file with your function and variable declarations. It will then appear in the Sketch | Import Library menu in the Arduino IDE.


2 Answers

I have been able to include a library in another Arduino library by using a relative path. For example, to include the AbstractSwitch library into the DigitalSwitch library, assuming that both of these libraries live in their own separate folders within Arduino's standard library folder, you can use the following include statement:

#include "../AbstractSwitch/AbstractSwitch.h"

In other words, your include statement should read:

#include "../LibraryFolder/LibraryHeaderFile.h"
like image 178
julioterra Avatar answered Oct 20 '22 00:10

julioterra


The documentation here https://github.com/arduino/Arduino/wiki/Build-Process states:

The include path includes the sketch's directory, the target directory (/hardware/core//) and the avr include directory (/hardware/tools/avr/avr/include/), as well as any library directories (in /hardware/libraries/) which contain a header file which is included by the main sketch file.

This means that if you #include "ReferencedLibrary.h" from your main sketch file, this causes that file's libraries directory to get added to the include path for other libraries to include. A bit of a hack but it does work on my Mac.

like image 45
nicolaskruchten Avatar answered Oct 19 '22 23:10

nicolaskruchten