1
votes

I'm trying to copy a couple of libraries I created to my local sketch folder, as instructed in the Arduino Library Tutorial

My folder structure is the following

Documents\Arduino\MySketch\
  MySketch.ino
  libraries\
    Timer\
      Timer.h
      Timer.cpp

I get the following error

fatal error: Timer.h: No such file or directory
#include <Timer.h>

Note that this works just fine if I move my libraries to the C:\Program Files (x86)\Arduino\libraries folder, but I really don't want to keep them appart from my source since I can not check them to my git repository.

I think I'm following the instructions given in the tutorial precisely. I just googled about this and found several similar problems, but no solution. It's supposed to be working in recent versions of the IDE (I'm on 1.8.5 on Windows 10).

I also tried to include the libraries using double quotes instead of angle brackets but I got the same error.

Can you please let me know how to fix this problem?

Thanks

2

2 Answers

0
votes

Recent versions of the Arduino IDE do recursive compilation of the src subfolder of the sketch folder. So to achieve your goal, you will want a folder structure that looks something like this:

MySketch
|_MySketch.ino
|_src
   |_Timer
      |_Timer.h
      |_Timer.cpp

Then the #include directive in the sketch should look like this:

#include "src/Timer/Timer.h"

It's quite common for Arduino libraries to use incorrect syntax for their internal #include directives. For example, Timer.cpp might contain this line:

#include <Timer.h>

That doesn't cause a problem when the library is installed normally but it will cause an error when you try to use the library bundled with a sketch. The solution is to edit the library to use the correct syntax:

#include "Timer.h"
0
votes

It seems to me that the solution looks fine if libraries/Timer is used only by this sketch. What if you have several projects {git_repo}/sketch_{n}/sketch_{n}.ino calling the same class Timer(.h, .cpp) ?

If you want to avoid code duplication (and you do), then you may put them into {git_repo}/libraries/. You will still be able to check them out into git. However, this is painful because it requires that each time you modify one file into the library, you need to :

  • delete the dir C:\Program Files (x86)\Arduino\libraries\Timer
  • reload the ".zip" (from {git_repo}/libraries/Timer) from the Arduino IDE

Very painful.

What you could do is create a symbolic link from {git_repo}/libraries/your_library to C:\Program Files (x86)\Arduino\libraries\your_library

On Ubuntu that would be :

cd ~/Arduino/libraries/ # where Arduino stores the libs
rm -rf Timer/ # deleting your library if exists    
ln -s ~/dev/code/[MY_PROJECT]/libraries/Utils/Timer Timer

The Arduino IDE will know your library exists, and would recompile it automatically if Timer.{h,cpp} (from your git_repo) was modified in between.