Sharing a header across several sketches

I have two sketches for two implementations of LoRa using different hardware. Because it's LoRa, the config for both needs to be identical. Solution: one header.h with the common elements for both. Both sketches would rely on the same header file when they compile.

I created in my sketchbook directory ../libraries/Rocket/src/RR_LoRa.h in keeping with the existing structure. Into both sketches went this line:
#include "../libraries/Rocket/src/RR_LoRa.h"

In one sketch everything compiles and uploads perfectly. When I place the cursor over then #include line in the editor, I see the filename and below it a fully enumerated path. Perfect.

In the other sketch, it won't compile, complaining thusly:
Compilation error: ../libraries/Rocket/src/RR_LoRa.h: No such file or directory

Roads that led nowhere:

(1) No, it's not a typo. I cut and pasted the line from one sketch to the other.

(2) In the unsuccessful sketch, when I edit the path, I get a dropdown (unnamed) that has the contents of some directory that includes /libraries/ and a bunch of other Arduino stuff. Nowhere on my Ubuntu machine is a directory I can find, hidden or not, that has the same collection.

It's as if one of my sketches has a defined working directory from which it navigates successfully to the header file. The other doesn't. Now, where can I specify a directory where the sketch is, or some other way for the compiler to find my header file?

#include "../libraries/Rocket/src/RR_LoRa.h"

Is that the actual line of code that you have in both sketches ?

Why not simply

#include <RR_LoRa.h>

like any other library ?

(1) Yes. That is the actual line of code in both sketches. It works perfectly in one. It fails miserably in the other.
(2) I tried including the file name alone, in quotation marks. It doesn't work. I suspect, but I don't know for sure, that a library directory created manually doesn't end up in the search tree unless it was (a) selected from the Arduino collection, or (b) in a .zip file that the Arduino library manager unpacked itself. And yes, I closed and restarted the Arduino IDE in case something special was happening at startup.

Hi @roberthadow

When you put the source code files under the src subfolder, this is what is known as the "1.5" library format. When you use this layout, it is mandatory to add a library.properties metadata file to the root of the library folder (libraries/Rocket/library.properties). If you don't do that, the Arduino library discovery system won't recognize the folder as an Arduino library and thus won't find the header file in the src subfolder.

You can learn what you need to put in a library.properties metadata file here:

https://arduino.github.io/arduino-cli/latest/library-specification/#library-metadata


If you don't want to mess with adding a library.properties metadata file, simply use the "1.0" library format (where a library.properties file is optional) by moving the header file to the root of the library folder (libraries/Rocket/RR_LoRa.h).


After you do that, you can simply add this line to any sketch in which you want to use the declarations from the header, just like any other library:

#include <RR_LoRa.h>
1 Like