6

How can I include libraries not contained in any direct subdirectories of a sketch, using the Arduino IDE?

I want my project structure to look like this:

Project
└> MCUa
 └> MCUa.ino
└> MCUb
 └> MCUb.ino
└> Libraries
 └> TimerLib
 └> TimerLib.cpp
 └> TimerLib.h

So in my project two sketches MCUa and MCUb share a resource, namely the library TimerLib.

I've tried to include the libraries like this:

#include "../Libraries/TimerLib/TimerLib.h"

or this:

#include "./../Libraries/TimerLib/TimerLib.h"

But none of them will compile - this is the error message:

fatal error: ./../Libraries/TimerLib/TimerLib.h: No such file or directory
 #include "./../Libraries/TimerLib/TimerLib.h"
 ^
compilation terminated.
exit status 1
Error compiling.

If I put the library directly in of the directories and reference it like this:

#include "./Libraries/TimerLib/TimerLib.h"

Everything goes well.

Using an absolute path like in this answer is not a possibility in this case, as it should compile out of the box for others, without modifying any absolute paths.

asked Sep 28, 2016 at 9:41
2
  • Did you try #include <TimerLib.h>? Add to sketch and in source of TimerLib. Commented Sep 28, 2016 at 9:46
  • @MikaelPatel If I put the TimerLib in the "global" Arduino directory, yes, that works. But I don't think I understand your comment/suggestion? What should be added to the source of TimerLib? Commented Sep 28, 2016 at 9:51

1 Answer 1

3

The problem with a relative path for an include is that it has to be relative to one of the predefined list of existing include directories. This includes:

  • Compiler include locations
  • The Arduino core files
  • The locations of other included libraries
  • The build folder for the sketch

What it does not include is the location of the sketch, so you cannot specify a location relative to the sketch itself.

Further, even if you could, the compiler wouldn't know what library it was supposed to be referencing so, while the header would be found, any source files associated with it would be ignored and linking would fail miserably.

Instead you will have to place the library in a standard library location, such as the libraries folder in your sketch book.

answered Sep 28, 2016 at 9:52
2
  • 1
    Ok, but why does #include "./Libraries/TimerLib/TimerLib.h" then work? That is relative to the sketch? I probably just don't understand completely how the compiler searches for libraries. Commented Sep 28, 2016 at 9:54
  • I suspect that it is being copied to the build folder along with the rest of the files in the sketch. Commented Sep 28, 2016 at 9:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.