C++ 将所有库保存在 Arduino 草图目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4705790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:12:12  来源:igfitidea点击:

Keeping all libraries in the Arduino sketch directory

c++cheaderarduino

提问by Adam Haile

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.

我知道您应该将任何外部库放在 arduino 安装目录的“libraries”文件夹下,但我有一个项目使用了我为该项目创建的几个库,主要是为了保持所有代码自包含和输出主 pde 文件。但是,我尝试将库与主 PDE 文件放在同一目录中,以便我可以更轻松地在 subversion 中保持所有内容同步(我在多台计算机上处​​理此问题)并且我不想继续分别备份和同步库。此外,只是为了能够轻松压缩草图文件夹并知道它包含所需的一切。

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.

我已经尝试将头文件作为新选项卡添加到草图中,但这似乎根本不起作用......甚至不在乎它们是否应该在 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.

这可能吗?而且,如果是这样,我如何将它们包含在构建的主文件中?最好在它们自己的子目录中。

回答by Uwe Hafner

I had the same issue. Solved it for Arduino IDE > 1.8. Seems a specialty in newer IDEs (?) according to the reference (see bottom link).

我遇到过同样的问题。为 Arduino IDE > 1.8 解决了这个问题。根据参考资料(见底部链接),似乎是较新的 IDE (?) 的专长。

You have to add a "src" Subdirectory before creating a library folder. So essentially your project should look like this:

在创建库文件夹之前,您必须添加一个“src”子目录。所以基本上你的项目应该是这样的:

/SketchDir (with *.ino file)  
/SketchDir/src  
/SketchDir/src/yourLib (with .h and .cpp file)  

and finally in your sketch you reference:

最后在你的草图中你参考:

#include "src/yourLib/yourLib.h"  

otherwise in my case - if I am missing the "src" folder - I get the error message that it cannot find the yourLib.cpp file.

否则在我的情况下 - 如果我缺少“src”文件夹 - 我收到错误消息,它找不到 yourLib.cpp 文件。

Note: I am using a windows system in case it differs and actually VS Code as wrapper for Arduino IDE. But both IDE compile it with this structure.

注意:我使用的是 Windows 系统,以防它不同,实际上 VS Code 作为 Arduino IDE 的包装器。但是两个IDE都使用这种结构编译它。

References: https://forum.arduino.cc/index.php?topic=445230.0

参考资料:https: //forum.arduino.cc/index.php?topic=445230.0

回答by KyleWpppd

For the sketches I have, the "*.h"and "*.cpp"library files actually reside in the same folder as the sketch, and I call them like "someheader.h". I also noticed that if I go into sketchmenu and add file...that the file doesn't appear until I close and reopen the sketch.

对于我拥有的草图"*.h""*.cpp"库文件实际上与草图位于同一文件夹中,我将它们称为"someheader.h". 我还注意到,如果我进入sketch菜单并且add file...文件在我关闭并重新打开草图之前不会出现。

回答by Tom Swirly

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 maketo put together a single .hfile from my .hand .cppsources - you can see this used in this Makefile:

为了解决这个问题,我曾经make.h我的.h.cpp来源中的单个文件放在一起- 你可以在这个 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 .cppor .ccfile in its directory it automatically uses it in the source, and you can't include anything that's not a .cpp, .ccor .h- so this is about the only way to do it.

Arduino 对文件结尾非常挑剔——如果你把一个.cpp.cc文件放在它的目录中,它会自动在源代码中使用它,你不能包含任何不是.cpp, .ccor 的东西.h——所以这是唯一的方法。

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

我使用了类似的伎俩也到JavaScript文件放在一起在这里

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

这要求您make在编辑文件后运行,但由于我无论如何都使用外部编辑器(Emacs),这对我来说是零麻烦。

回答by Timmmm

Unfortunately the Arduino IDE is awful and shows no signs of improving. There is no real build system so it only lets you build programs that reside in a single directory.

不幸的是,Arduino IDE 很糟糕,而且没有任何改进的迹象。没有真正的构建系统,所以它只允许您构建驻留在单个目录中的程序。

The only real solution is to write a makefile, then you can use a real IDE. I'm hopeful that one day someone will write an Arduino plugin for QtCreator.

唯一真正的解决方案是编写一个makefile,然后您就可以使用真正的IDE。我希望有一天有人会为 QtCreator 编写一个 Arduino 插件。

Here's an example makefile:

这是一个示例生成文件:

http://volker.top.geek.nz/arduino/Makefile-Arduino-v1.8

http://volker.top.geek.nz/arduino/Makefile-Arduino-v1.8

回答by sdaau

I just had this same problem (I also like to keep the code self-contained), so I'll just jot down some notes; say I have a MyPdeSketch.pdeusing MyLibClass.cpp; then I have it organized like this

我刚刚遇到了同样的问题(我也喜欢保持代码独立),所以我会记下一些笔记;说我有一个MyPdeSketch.pde使用MyLibClass.cpp;然后我把它组织成这样

/path/to/skdir/MyPdeSketch/MyPdeSketch.pde
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.h

(In principle, /path/to/skdir/here is equivalent to ~/sketchbook/)

(原则上,/path/to/skdir/这里相当于~/sketchbook/

 

 

What worked for me is something like:

对我有用的是:

mkdir /path/to/arduino-0022/libraries/MyLibClass
ln -s /path/to/skdir/MyPdeSketch/MyLibClass/MyLibClass.* /path/to/arduino-0022/libraries/MyLibClass/

After restart of the IDE, MyLibClassshould show under ''Sketch/Import Library''.

IDE 重新启动后,MyLibClass应显示在“草图/导入库”下。

Note that the only way I can see so far for a library class file to refer to other library files is to include them relatively (from 'current location'), assuming they are all in the same main arduino-0022/librariesfolder (possibly related Stack Overflow question: Is it possible to include a library from another library using the Arduino IDE?).

请注意,到目前为止,我可以看到库类文件引用其他库文件的唯一方法是相对地包含它们(从“当前位置”),假设它们都在同一个主arduino-0022/libraries文件夹中(可能与堆栈溢出问题有关:是否可以使用 Arduino IDE 包含来自另一个库的库?)。

Otherwise, it should also be possible to symlink the MyLibClassdirectory directly into arduino-0022/libraries(instead of manually making a directory, and then symlinking the files). For the same reason, symlinking to the alternate location ~/sketchbook/librariescould also be problematic.

否则,也应该可以MyLibClass直接将目录符号链接到arduino-0022/libraries(而不是手动创建目录,然后符号链接文件)。出于同样的原因,符号链接到备用位置~/sketchbook/libraries也可能有问题。

Finally, a possibly better organization could be:

最后,一个可能更好的组织可能是:

/path/to/skdir/MyLibClass/MyLibClass.cpp
/path/to/skdir/MyLibClass/MyLibClass.h
/path/to/skdir/MyLibClass/MyPdeSketch/MyPdeSketch.pde

... which, after symlinking to libraries, would force MyPdeSketchto show under the examples for the MyLibClasslibrary in Arduino IDE (however, it may not be applicable if you want to self-contain multiple class folders under a single directory).

...在符号链接到 后libraries,将强制在 Arduino IDE 中MyPdeSketchMyLibClass库示例下显示(但是,如果您想在单个目录下自包含多个类文件夹,它可能不适用)。

EDIT: or just use a Makefile- which would work directly with avr-gcc, bypassing the Arduino IDE (in which case, the sketchbook file organization can be somewhat loosened)..

编辑:或者只是使用Makefile- 它可以直接使用avr-gcc,绕过 Arduino IDE(在这种情况下,草图文件组织可以稍微松散)..

回答by M.Hefny

Think I know what do u need exactly.

我想我知道你到底需要什么。

you have a project folder say MYPROJ_FOLDERand you want to include a Librariesfolder that contains more children folders for your custom libraries.

您有一个名为 MYPROJ_FOLDER的项目文件夹,并且您希望包含一个Libraries文件夹,该文件夹包含更多用于您的自定义库的子文件夹。

you need to do the following: 1- create folders as follows:

您需要执行以下操作: 1- 创建文件夹如下:

-MyProjFolder
-MyProjFolder/MyProjFolder        

and then create a file with the folder name in .ino extension -MyProjFolder/MyProjFolder/MyProjFolder.ino

然后使用 .ino 扩展名中的文件夹名称创建一个文件 -MyProjFolder/MyProjFolder/MyProjFolder.ino

2- create libraries folder: -MyProjFolder/libraries <<<<< name is not an option should be called like that.

2- 创建库文件夹: -MyProjFolder/libraries <<<<< name 不是一个应该这样调用的选项。

3- then create your own libraries -MyProjFolder/libraries/lib1-MyProjFolder/libraries/lib1/lib1.cpp -MyProjFolder/libraries/lib1/examples<<<< this is a folder -MyProjFolder/libraries/lib1/examples/example1

3- 然后创建自己的库 -MyProjFolder/libraries/ lib1-MyProjFolder/libraries/lib1/lib1.cpp -MyProjFolder/libraries/lib1/ examples<<<< 这是一个文件夹 -MyProjFolder/libraries/lib1/examples/ example1

repeat step 3 as much as you want

尽可能多地重复步骤 3

also check http://arduino.cc/en/Guide/Libraries

还要检查http://arduino.cc/en/Guide/Libraries

回答by RaistlinMolina

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.

对我有用的是在草图目录下创建一个目录,例如“src”,并在该目录下为每个个人库创建一个目录。

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"

我有一个名为 ObstacleRobot 的项目,在我的草图文件夹下,名为障碍机器人(由 IDE 自动创建)和我的草图“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.

然后我想包含一个与这个项目完全相关的个人库,并且将它包含在 IDE 库中没有任何意义,事实上我想为机器人的每个部分都这样做,但我仍在努力。

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:

#include "src/Sonar/Sonar.h"

And thats all.

就这样。

回答by Victor von Cacahuete

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

按照 Hefny 的思路,让你的项目成为你图书馆的一个例子。

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

例如(Unix env),假设库在 ~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

你创建你的项目 ~arduino/libraries/MyProject,你的库去那里(例如 ~/arduino/libraries/MyProject/module1.h ~/arduino/libraries/MyProject/module1.cpp ~/arduino/libraries/MyProject/module2.h ~/arduino/libraries/MyProject/module1.cpp。 h ~/arduino/libraries/MyProject/module2.cpp

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

现在: 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)

编辑 ~arduino/libraries/MyProject/examples/myproj/myproj.ino(注意这不是 examples/myproj.ino 而是 examples/myproj/myproj.ino)

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

重新启动 IDE,您应该在菜单 File/Example/MyProject 中找到您的项目。

Also note that you do the include with #include

另请注意,您使用 #include 执行包含

回答by Joao

Why dont we just write a script with a single copy command, copying our libs from wherever our library is located into the arduino IDE library folder?

为什么我们不只用一个复制命令编写一个脚本,将我们的库从我们的库所在的任何地方复制到 arduino IDE 库文件夹中?

This way we keep the file structure we want and use the IDE library requirements without fuss.

这样我们就可以保持我们想要的文件结构,并且可以毫不费力地使用 IDE 库要求。

Something like this works for me:

像这样的东西对我有用:

cp -r mylibs/* ~/Documents/programs/arduino-1.5.8/libraries/.

Note that the paths are relative to my own file structure.

请注意,路径是相对于我自己的文件结构的。

Hope this helps someone. This includes my future self that I bet will be reading this in a near future... as usual!

希望这可以帮助某人。这包括我未来的自己,我打赌我会在不久的将来阅读这篇文章……像往常一样!

J

J