In the sketch project-03/libtest.ino, I want to load to a header file (and the library source code) of a customized library (or even my own library) (just for test purposes).
My question is, how should the include statement look like to access the header file (and library source in directory: projects/custom-libraries/custom/header-01.h
With other works, how is the IDE handling during an include the path to another directory from the current project folder?
Should it be something like #include “../custom-libraries/custom-01/header-01.h”?
I tested already the menu option: Sketch → Add File. But this is adding the file(s) to the project folder. Therefore there is a duplication of code, which I do not want.
I want to maintain my customized library location and when needed, referring to it with the #include statement.
Typically an #include directive for a library header file in an Arduino sketch program has two effects:
Adds the content of the specified file into the program.
Causes the source files of the library that contains the specified file to be compiled.
You can specify any path you like in an #include directive. However, the latter of those two effects only occurs when no path is specified in the #include directive.
For example, if you have a library like this installed:
Relative paths outside the sketch folder won't work as expected in the code of the sketch files. The reason is the sketch is copied to a temporary folder before being compiled. The relative path that is correct for the real location of the sketch will not be correct for the location in the temporary build folder.
You should put the customized library in the libraries subfolder of the sketchbook folder. I you do that, you can use the library in your sketches as usual.
If there is some reason why you don't want to place the library in the standard location, please provide a detailed explanation. That information will allow use to effectively advise you on how you can accomplish your goal.
Note this requires that the sketch has been saved in the sketchbook; it won't work for a new/temp sketch. Then the include in the .ino (or any file in the sketch root) would be
#include "src/custom-pi/custom-pi.h"
which is a little long, but it works.
In the Preferences, enable Show files inside sketches to see the source tree in the Sketchbook.
Arduino was made to make easy parts extra easy at the price of hard parts be extra hard.
When you are beginer, it is nice to scratch a sketch and be done and see it does something somehow.
I bought the larger kit many ears before and started by doing all the examples inside, so I get fast start and learned a lot fast without all the hurdles the wiring hide from me. It was really good.
I later start do larger and larger project and first I went out from IDE to get some better editor (VIM in my case) and some other tools I wanted (GIT - I cannot recomend it enought, it is really must have for any project), and some years later I started to use Arduino-CLI and another CLI based tools, my projects became .cpp (with #include “Arduino.h”) instead of .ino, then later I left even the #include “Arduino.h” after me and now I use make with Makefile and I use lot of assembler (some thing are really faster there and some are even easier - and some not)
I still sometime use Arduino IDE, mainly for fast test like blink, or some library for some HW or so, but it is just one of many tools, with its pluses and minuses.
First of all, thanks you for the detailed response.
Here is the reason why I want to use a location other than the default libraries directory:
I have already a ‘standard” library (ILI9488) in the default libraries directory.
Currently I’m using a modified version of this library for a few different projects (at the same time due to different mcu’s). I don’t want to interfere with the ‘standard’ version of the library (keep it unchanged).
If I would store the modified version in the library directory, I will have a name conflict.
In addition, I don’t want to change names of include files during the test phase of the modified library.
That’s why my first thought was to use a relative path in the #include statement. But as you’ve explained that doesn’t applies to the library code file (cpp).
(Why even copying everything to a temporary location during compilation?)
I think here also would help if the IDE would utilize an include-path environment var or at least a Preference property (like the sketch book folder).
Perhaps I’m on the absolute wrong path of thoughts on this matter, but that’s where I am right now.
Arduino IDE is designed to be friendly to new users through a carefully curated user interface that only offers features that will be useful to a significant portion of the user base. Although you are completely reasonable to want this feature for your own use case, it would be of no interest to 99.9% of users.
We also offer an alternative development tool that is specifically targeted to meet the needs of advanced users: Arduino CLI. You can specify arbitrary additional libraries paths when compiling sketches using Arduino CLI:
--libraries strings Path to a collection of libraries. Can be used multiple times or entries can be comma separated.
--library strings Path to a single library’s root folder. Can be used multiple times or entries can be comma separated.
This approach would allow you to use the standard approach with your #include directives, only specifying the header filename and allowing Arduino CLI to discover the library as usual.
But if you (understandably) prefer to use Arduino IDE, the solution offered by @kenb4 will be an excellent one.
Perhaps I will look into the solution via CLI, in particular I'm using KATE as code editor very often and I could compile from inside the editor. That may also enable an utilization of the Make tool.
Overall, this thread is a nice piece of learning.
Thanks for that to all contributors of this thread.