I have two Arduinos configured as master and slave using I2C. I've defined a set of commands so I need both sketches to be able to access the command definitions (a combination of structs and enums). Obviously I can just have two copies of the include file but I'm trying to avoid this. It looks like the IDE doesn't support relative paths for include files (is this really true?).
What should I do here? It appears I can create my own library but I don't want these definitions to be visible to any other projects. Also, I want to be able to use the sketches on different computers and for different users so using absolute paths isn't going to work.
In case it's not clear, what I was expecting to be able to use is:
OK so I guess if it's in a sub-directory I can just add another '../'. Or is it somewhere else?
That's really bad practice. Yes, I know it 'works' but if there are multiple projects, it's just so easy to end up with the wrong file being included. Think about what might happen if you decide to rename a project for example.
It is under your operating system's temporary folder.
Try this:
Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
The "Preferences" dialog will open.
Check the box next to "Show verbose output during: ☐ compilation" in the "Preferences" dialog.
Click the "OK" button.
Compile the sketch as usual.
Wait for the compilation to finish.
Now examine the contents of the black "Output" panel at the bottom of the Arduino IDE window. There you will see the commands the IDE executed to compile the sketch, including the path of the temporary build folder.
For example, from this line of my verbose compilation output:
It is how every Arduino library is used. Thousands of these libraries have been used in tens of millions of sketches over the last two decades.
Does the wrong library occasionally get picked by the Arduino sketch build system? Yes, but from over a decade of providing support to the Arduino community with a special focus on just this type of thing I can tell you it is fairly rare. It is usually the result of library authors using generic primary header filenames or making a distinct hard fork, but continuing to use the same filenames as the parent library.
I can't think of any problem this would cause. The project name is irrelevant. The relevant part is the filename of the library's primary headers.
Only if you #include the wrong one. You might just as well say the same about any library
If you rename a project then you can, of course, still #include the same library file. Giving the library file a sensible name will help and is good practice
After a lot of experimentation, I think the solution is to use the 'Sketch->AddFile' to add the include file to the project. It looks like this gets copied before compilation. (I find all this copying rather strange as I'm used to working on large codebases where copying everything would be a complete no-no, but I guess it works for Arduinos).
Not sure we're talking about the same thing here. A library is what you use when you want to expose common functionality to multiple applications. The header file I need to include is private to the app and so shouldn't be visible to other applications and so should not be in a library. Essentially, it's just following the principle of encapsulation: only make visible what needs to be visible.
The copying I'm referring to is the way the IDE copies source files before compiling them. I don't really have a problem with that as the IDE needs to add boiler-plate code before it will compile. It just feels a little strange as I'm used to working with native C++.
A library is what you use when you want to expose common functionality to multiple programs. Whether the programs are the same app or not is irrelevant
It won't be visible unless you #include it will it ?