How to manage dependencies?

Arduino is meant to be easy way for non-technical people and so it hides as much as it can from plain view to be simple.

For simple project it works at the cost of overhead of copying libraries here and there and using Arduino's "IDE". Calling programs "sketches" and manipulate it to hide, that it is in C++. And so on and so on.

If you want to make more complicated projects, version control, good environment and such, you need to leave "simply Arduino way" and start using more powerful and complicated tools.

I personally use https://github.com/sudar/Arduino-Makefile https://github.com/ladislas/Bare-Arduino-Project and have it in GIT repositories.

This way I can share libraries between projects in group, also links to libraries outside the tree works, as well as gits submodules, both allowing you to have updated libraries from 3rd party aviable to all projects, also those old ones.

I still use a lot of Arduino (like setup/loop, digitalRead and such), but also I use "normal" g++ constructs on the way. Later I will diverge even more to plain C/g++/.. code probably, using some RTOS or so, but now my needs are not so far yet. (google "arduino rtos" I am currently reading https://github.com/greiman/NilRTOS-Arduino )

Arduino is not meant to be "best practice" anyway. It is meant to be "fast and easy way to do something, when you know nothing". And it works, as anybody and his dog can start with Arduino and if interested, he can go more far on his own, or he can stick with easy and have fun anyway.


If you want the project to be self contained you can put the library dependencies in the sketch folder. So the folder structure of the Foo sketch with the Logging library dependency would look something like this:

Foo
|_Foo.ino
|_src
  |_Logging
    |_Logging.h
    |_Logging.cpp

And in Foo.ino you will include the library like this:

#include "src/Logging/Logging.h"

Unfortunately some libraries use the incorrect include syntax which still works when the library is installed in one of the libraries folders but not when located in the sketch folder. The Logging library does use the correct include syntax but sometimes to put libraries in the sketch folder you will need to fix this. For example, let's say you have a library named Bar and in Bar.cpp you see this:

#include <Bar.h>

That won't work because if installed in the sketch folder Bar.h will not be located in one of the standard include paths so you need to edit Bar.cpp and change the include syntax to:

#include "Bar.h"

Which will cause the local folder to be searched for the included file.

The downside to this system is that you may end up with multiple copies of a library so it's more work to update or modify each copy. Of course that can also be a benefit because each project can have its own known-working version of the library that will not be affected by any updates you may make to the same library used in other projects.

I generally just add instructions for how to install library dependencies in the usual manner to the documentation for a project. That should be within the capabilities of the average Arduino user. If a specific version of a library is necessary then you document that. However, there are cases where I have wanted to hand over a self-contained project to someone who is not familiar with the use of the Arduino IDE and that is when I have used the system described above for all necessary 3rd party libraries.

Tags:

Library