I have a project that I have a custom library that I have.
/sketch/project/main_project.ino
and my library is located here:
/sketch/project/library/lib.h
/sketch/project/library/lib.cpp
I tried:
#include <lib.h>
and:
#include "lib.h"
But both fail with:
fatal error: lib.h: No such file or directory
Now I read up on subdirectories, and I have read that src/ and libraries/ are both special cases and the compiler should be able to find the library I included.
What is the solution here?
ZX80
January 19, 2023, 11:49pm
2
Would it be a big problem if you put your library in the same folder as the other libraries ?
1 Like
src
, yes:
https://arduino.github.io/arduino-cli/latest/sketch-specification/#src-subfolder
But there is no special treatment of a libraries
subfolder by the Arduino development software.
Put the library under the src
subfolder of the sketch.
Use a relative #include
directive to the library in your sketch:
#include "src/library/lib.h"
1 Like
The reason is that I want to distribute dependencies with my repo. Instead of share libraries, I want to include a specific version
@ptillisch that just works, thank you so much for pointing me in the right direction here. I was trying and trying. I assumed that you didn't need to include the path, but your example makes it clear it needs too.
It's too bad you can't just include a libraries folder in your main sketch folder and than that is included when looking for libraries. But this works for me.
You are welcome. I'm glad it is working now.
There is a formal feature request for this here:
opened 06:01PM - 04 Jul 18 UTC
type: enhancement
topic: code
### Describe the request
🙂 It will be easier for sketch developers to share a… sketch as a package that includes library dependencies, avoiding the need for each user to install the library dependencies.
🙂 Library dependencies of the sketch can be controlled, allowing them to be pinned to the specific versions the sketch was intended to be used with, independent from the version of any globally installations of the library. As an example there are major changes to the API of the very popular "**ArduinoJson**" library in version 6 and sketches only work with one or the other major version series of that dependency.
### Describe the current behavior
The common approach to bundling libraries with a sketch is to dump the library code files into the sketch root folder.
### Arduino CLI version
a58d5adbe269ecc55778c007dc8ac39ffd42ce85
### Operating system
All
### Operating system version
All
### Additional context
1. As a workaround in the preferences the sketchbook location ([`directories.user`](https://arduino.github.io/arduino-cli/dev/configuration/#configuration-keys)) can be set to the sketch folder. But this has to be done manually and changed every time another program is handled. So there could be some option for this to be set in the `.ino` file.
2. In the build folder a file `build.options.json` is generated automatically.
There are some options where additional library folders could possibly be added?
E.g.:
- builtInLibrariesFolders
- otherLibrariesFolders
#### Additional Requests
- https://github.com/arduino/arduino-ide/issues/150#issuecomment-830714850
- https://github.com/arduino/arduino-cli/issues/1255#issuecomment-568900798
- https://github.com/arduino/arduino-cli/issues/1255#issuecomment-671084645
- https://github.com/arduino/arduino-cli/issues/1255#issuecomment-751318119
- https://github.com/arduino/arduino-cli/issues/1255#issuecomment-750951958
- https://forum.arduino.cc/t/how-do-i-include-a-library-in-a-sketch-directory/1079812
- https://forum.arduino.cc/t/arduino-ide-cant-find-local-custom-libraries-in-relative-path/669421
- https://forum.arduino.cc/t/suggestion-improve-library-manager/847666
- https://forum.arduino.cc/t/using-generated-headers/912691
### Issue checklist
- [X] I searched for previous requests in [the issue tracker](https://github.com/arduino/arduino-cli/issues?q=)
- [X] I verified the feature was still missing when using the latest [nightly build](https://arduino.github.io/arduino-cli/dev/installation/#nightly-builds)
- [X] My request contains all necessary details
(Arduino CLI provides the library discovery and compilation for Arduino IDE 2.x)
system
Closed
July 20, 2023, 11:13am
7
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.