Hey,
I wrote a small project with several additional header and .cpp files in my sketch folder for better code organization. I also used different libraries, e.g. FastLED and a sound player library from GitHub. I installed them normally via the Arduino IDE 2.3.4 on my Windows PC.
Then I copied my sketch folder to a USB stick and tried to compile it on my MacBook Air. However, it didn’t compile because the includes weren’t resolved correctly.
I searched the web and found various suggestions.
So, what’s the proper way to handle this? I tried copying the external libraries into a libraries folder inside my sketch folder, but it still didn’t compile. I also changed some #include "stuff.h" to #include <stuff.h> without success.
Do I have to rename the libraries folder to src or _src and include the libs with full paths, like #include <src/libs/FastLED.h>? Or is there a smoother, more standard solution?
The simplest way to do what you want is to put all of the library files in the sketch folder that use the #include "LibraryName.h" format of the #include
That's the other way around ➜ including a header with <xxx.h> directs the preprocessor to search only the system include paths, which are predefined folders for standard and shared libraries.
Using "xxx.h" makes the preprocessor first search the current working directory (the sketch folder in Arduino), then the additional user-defined include paths, and finally the system include paths.
This ordering allows local or project-specific headers to override system ones if they share the same name.
So When a sketch compiles fine with <xxx.h>, it means the Arduino build system is finding those libraries in its global search paths (core libraries, hardware packages, or the libraries/ folder (the one you see in your Arduino home, not inside the sketch itself)). To make the project portable, you need to place every non-core library inside a libraries subfolder of your project directory, and switch your includes from <xxx.h> to "xxx.h". That way the preprocessor will look first in your project’s local libraries folder, ensuring the project can be copied and built elsewhere without depending on the global installation.
The easiest way is to use portable installations of IDE 1.x on both the Mac and the Windows PC. You will need to create a directory called "portable" at the same level as the executable; that directory will contain your sketchbook and installed libraries. You can copy that "portable" directory
The alternative is to create a directory called "src" in the sketch directory and copy the needed libraries in there. You can include with e.g. #include "src/FastLed.h".
Be aware that in a library other files might still be included using <...> and you need to fix all of them.
Notes:
I'm not a Mac user so not sure if protable installations can be done.
I only use the "src" directory for my own "libraries", never tried with 3rd party libraries.
For now, the best solution that is usable with Arduino IDE is to place the libraries under the src subfolder of the sketch. If you run into any problems while doing that, just let us know and we'll provide further assistance.
The folder must be named exactly src.
If you don't mind compiling your sketches via the command line, you can use the "build profiles" feature of the official Arduino CLI tool:
Unfortunately build profiles are not currently supported by Arduino IDE. The Arduino developers are tracking the request for adding build profile support to Arduino IDE here:
Please only comment on the GitHub issue thread if you have new technical information that will assist with the resolution. General discussion and support requests are always welcome here on the Arduino Forum.
yeah i already did that for my self created files. i wanted to encapsulate as much as possible and so i ended up with around 20 files until know. i think for better structure and hide e.g. class implementations i already wrote and don’t want to modify it’ll be cleaner thatthese files don’t appear when opening the ino files.
thank you’ll try this approach so everything that is in the sketch folder and there in the src folder will be looked up automatically and is not depending on folder depth?
the main thing i want to archive, also fir future projects is that i can store a fully compilable sketch without needing to always look up the necessary libs and install them.
i’ll try your approach. the fall back option will be that i’ll create a folder like “needs to be installed” where i’ll place all libs that need to be installed before the sketch can be compiled.
Arduino IDE recurses through all subfolders of the src folder and compiles all the source files.
However, you must specify the relative path to the header files in any #include directives. As @sterretje mentioned, in addition to the #include directives in your sketch files, this also applies to #include directives in the library files. If the library developer used the appropriate syntax in their #include directives, those targeting header files within the library will already be relative and so those won't need any adjustment. However, if the library contains any #include directives for header files in other libraries you have bundled with the sketch, those will definitely need to be adjusted. And unfortunately it is very common for library developers to use the inappropriate angle bracket syntax in their #include directives for local header files, so you may find that some of those #include directives in libraries may need to be changed to the correct double quote syntax.
Makes it hard to do "version control" of the libraries, or distribution to less competent "customers."
I mean, every time someone asks about protecting or distributing binaries, the advice is to just send out the source. But then that requires "install this version of the IDE, and these versions of these four libraries", it's a bit awkward.
It would be nice to be able to eliminate at least some of those steps.
(of course, this isn't just a problem with Arduino; trying to install open source software in many venues can send one down "dependencies hell.")