How to share code among different sketches in a multi-developer environment?

I am working on an Arduino project with other developers. We have a shared git repository whose folder structure looks like this:

 - TopLevelProjectFolder
   - Sketch1
      - Sketch1.ino
   - Sketch2
      - Sketch2.ino
   - ...
   - Utils
      - Func1.h
      - Func1.cpp
      - Func2.h
      - Func2.cpp
      - ...

Each .ino sketch file makes use of one or more of the functions defined in the common Utils directory.

We are using the Arduino IDE to build our executables. I understand that there are two standard ways that I can include custom code in a .ino file so that the Arduino IDE can find it:

  1. Create a src folder beneath each sketch folder and manually copy the required utility functions (.cpp and .h) from Utils to this new folder; then include them in the .ino files using #include "src/Func1.h" directives.

  2. Create a Utils folder beneath the machine-specific Arduino/Libraries folder (created during installation of the Arduino IDE) and copy the contents of the Utils directory in the repo to that folder.

Neither of these is ideal in a multi-developer environment: they each involve manual duplication of files (to the respective target directory) and potential mishaps when updating shared utility functions. I tried to automate copying files from Utils to the target directory using pre-build hooks defined in platform.local.txt but they take effect too late in the build process (and in any case this isn't great because platform.local.txt has to live in the machine-specific installation directory).

What is the recommended way to share common code with a set-up like this?

Perhaps the utility functions could be encapsulated in a single library. The user would #include the needed functions.

Thanks for your reply, John. Can you give me a quick example to illustrate what you mean?

Create a symlink/alias of the Utility folder in the 'libraries' folder inside your sketchbook. This should show up as a library after you restart the IDE (or run Tools -> Manage Libraries... briefly). Then see if your sketches can #include the Func1.h, Func2.h, etc directly.

You may need a "library.properties" file listing the available .h files. I don't know. Worst case: create an empty "Utils.h" inside the "Utils" folder. Adding a #include <Utils.h> should then be enough to put the Utils library in the include search path for the compile.

Thanks very much, John - I've now tried that and it does indeed work. I'm a little surprised that one has to do this (go to the trouble of creating symlinks) in order to share code efficiently in a multi-developer setting; but you have solved my problem and for that I am truly grateful!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.