Keeping all libraries in the Arduino sketch direct

2019-03-11 14:47发布

I know that you are supposed to place any external libraries under the "libraries" folder of the arduino install directory, but I have a project that uses several libraries that I have created for the project and mainly to keep all that code self contained and out of the main pde file. However, I have tried to place the libraries in the same directory as the main PDE file so that I can more easily keep everything synced up in subversion (I work on this on multiple computers) and I don't want to have to keep going back and syncing up the libraries separately. Also, just for the sake of being able to easily zip of the sketch folder and know that it contains everything it needs.

I've tried adding the header files to the sketch as a new tab, but that doesn't seem to work at all... don't even care if they should up in the arduino IDE.

I've also tried adding the libraries to the sketch directory in subdirectories (what I would greatly prefer) and then linking to them as:

#include "mylib/mylib.h"

and

#include <mylib/mylib.h>

But both of these result in file not found errors.

Is this possible? And, if so, how do I include them in the main file for building? Preferably in their own subdirectories.

9条回答
forever°为你锁心
2楼-- · 2019-03-11 15:37

I agree with you; this is an intolerable way to develop software: it requires every file that you need to be in the same directory as the main program!

To get around this, I use make to put together a single .h file from my .h and .cpp sources - you can see this used in this Makefile:

PREPROCESS=gcc -E -C -x c -iquote ./src
# -E : Stop after preprocessing.
# -C : Don't discard comments.
# -x c : Treat the file as C code.
# -iquote ./src : Use ./src for the non-system include path.

TARGETS=sketches/morse/morse.h

all: $(TARGETS)

clean:
    rm $(TARGETS)

%.h: %.h.in
    $(PREPROCESS) $< -o $@

Arduino is very picky about file endings - if you put a .cpp or .cc file in its directory it automatically uses it in the source, and you can't include anything that's not a .cpp, .cc or .h - so this is about the only way to do it.

I use a similar trick also to put together JavaScript files here.

This requires that you run make after editing your files, but since I'm using an external editor (Emacs) anyway, this is zero hassle for me.

查看更多
该账号已被封号
3楼-- · 2019-03-11 15:38

Following the lines of Hefny, make your project an example for your library.

For example (Unix env), let's say the libraries are in ~arduino/libraries

Your create your project ~arduino/libraries/MyProject, your libraries go there (for example ~/arduino/libraries/MyProject/module1.h ~/arduino/libraries/MyProject/module1.cpp ~/arduino/libraries/MyProject/module2.h ~/arduino/libraries/MyProject/module2.cpp

Now: mkdir -p ~arduino/libraries/MyProject/examples/myproj

edit ~arduino/libraries/MyProject/examples/myproj/myproj.ino (note that this is not examples/myproj.ino but examples/myproj/myproj.ino)

Restart the IDE, you should find your project in the menu File/Example/MyProject.

Also note that you do the include with #include

查看更多
地球回转人心会变
4楼-- · 2019-03-11 15:39

What has worked for me is to create a dir, for example "src" under the sketch dir, and under that a dir for each personal library.

Example:

I have a project called ObstacleRobot, under that a folder for my sketch, named obstaclerobot (automatically created by the IDE) and there my sketch "obstacleRobot.ino"

Up to now we have:

 /ObstacleRobot
    /obstaclerobot
       obstacleRobot.ino

Then I wanted to include a personal library that was fully related with this project and made no sense in including it in the IDE libraries, well in fact I want to do this for each part of the robot but I'm still working on it.

What in the end worked for me was:

 /ObstacleRobot
    /obstaclerobot
       obstacleRobot.ino
       /src
          /Sonar
             Sonar.h
             Sonar.cpp

Then what you have to do in the main sketch is to write the include as follows:

#include "src/Sonar/Sonar.h"

And thats all.

查看更多
登录 后发表回答