Arduino Library-to-Library Dependencies

I’m building a series of Arduino libraries that are meant to work together. I was quite surprised to learn that there’s no straightforward and well-documented way to indicate that one Arduino library depends on another! Instead, users currently need to manually manage dependencies by installing the proper libraries.

Planned Dependency Support

It seems that Arduino is currently planning library dependency support in a future release of the IDE. Details can be seen in this pull request and this issue.

The gist is, you can add a depends entry to your library.properties file, which references the registered name of another library. Multiple libraries can be comma-separated. Here’s an example from the Arduino_OAuth project:

name=Arduino_OAuth
version=0.1.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=[BETA] OAuth 1.0 client library for Arduino. 
paragraph=This library depends on ArduinoHttpClient and ArduinoBearSSL.
category=Communication
url=http://github.com/arduino-libraries/Arduino_OAuth
architectures=*
includes=Arduino_OAuth.h
depends=ArduinoHttpClient,ArduinoBearSSL

More information about the library.properties file can be found on the Arduino GitHub wiki, although dependencies are not documented at the time of writing this article.

Manual Install Management

Until the IDE with dependency support is released, the best way to manage inter-library dependencies in the Arduino ecosystem is to include a note in your README indicating which other libraries are required.

Luckily, the IDE has improved enough such that a library can include a header from another Arduino library, and if it is present on the user’s system it will be included automatically in the build.

That means you can manage the header inclusion within your library. The user no longer has to manually include the dependent library in their sketch.

If I have a library called arduino-logger, and I want ArduinoPrintf to be used within that library, I just need to include the ArduinoPrintf.h header somewhere in my library:

#ifndef ARDUINO_LOGGER_H_
#define ARDUINO_LOGGER_H_

#include "ArduinoPrintf.h" // Dependent library header

// Code here

#endif // ARDUINO_LOGGER_H_

As long as the arduino-printf library is installed on my system, the Arduino IDE will automatically compile the other library when ArduinoLogger.h is included in my sketch:

Using library arduino-logger in folder: /Users/pjohnston/Documents/Arduino/libraries/arduino-logger
Using library arduino-printf in folder: /Users/pjohnston/Documents/Arduino/libraries/arduino-printf

Further Reading

Share Your Thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.