How do I include a library in a sketch directory

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?

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:

(Arduino CLI provides the library discovery and compilation for Arduino IDE 2.x)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.