C++ 是否可以使用 Arduino IDE 从另一个库中包含一个库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6504211/
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 20:17:19  来源:igfitidea点击:

Is it possible to include a library from another library using the Arduino IDE?

c++ideincludearduino

提问by Robert Atkins

I'm trying to write an Arduino library (effectively a C++ class) which itself references another library I have installed in my Mac's ~/Documents/Arduino/libraries directory.

我正在尝试编写一个 Arduino 库(实际上是一个 C++ 类),它本身引用了我在 Mac 的 ~/Documents/Arduino/libraries 目录中安装的另一个库。

At the top of the .cpp of the library I'm writing, I've tried

在我正在编写的库的 .cpp 顶部,我尝试过

#include <ReferencedLibrary.h>

and

#include "ReferencedLibrary.h"

... neither of which work. I can successfully #include <ReferencedLibrary.h>from sketches in my ~/Documents/Arduino directory. Am I missing something or is this a limitation of the Arduino IDE/makefile? Is there a workaround?

......两者都不起作用。我可以成功地#include <ReferencedLibrary.h>从我的 ~/Documents/Arduino 目录中的草图。我是否遗漏了什么或者这是 Arduino IDE/makefile 的限制?有解决方法吗?

采纳答案by agakshat

This issue was solved in the Arduino 1.6.6 release. The release notes of 1.6.6 mention that library to library dependencies have been fixed.

此问题已在 Arduino 1.6.6 版本中解决。1.6.6 的发行说明提到库到库的依赖关系已得到修复。

Library to library dependencies: when your sketch imports a library, and that library uses another, the IDE will find out without you having to add a useless #include to your sketch

库到库的依赖关系:当您的草图导入一个库,而该库使用另一个库时,IDE 会发现您无需向草图添加无用的 #include

Updating your version to 1.6.6 or newer will resolve your problem.

将您的版本更新到 1.6.6 或更高版本将解决您的问题。

回答by julioterra

I have been able to include a library in another Arduino library by using a relative path. For example, to include the AbstractSwitch library into the DigitalSwitch library, assuming that both of these libraries live in their own separate folders within Arduino's standard library folder, you can use the following include statement:

我已经能够通过使用相对路径在另一个 Arduino 库中包含一个库。例如,要将 AbstractSwitch 库包含到 DigitalSwitch 库中,假设这两个库都位于 Arduino 标准库文件夹中各自独立的文件夹中,您可以使用以下 include 语句:

#include "../AbstractSwitch/AbstractSwitch.h"

In other words, your include statement should read:

换句话说,您的包含声明应为:

#include "../LibraryFolder/LibraryHeaderFile.h"

回答by nicolaskruchten

The documentation here https://github.com/arduino/Arduino/wiki/Build-Processstates:

此处的文档https://github.com/arduino/Arduino/wiki/Build-Process指出:

The include path includes the sketch's directory, the target directory (/hardware/core//) and the avr include directory (/hardware/tools/avr/avr/include/), as well as any library directories (in /hardware/libraries/) which contain a header file which is included by the main sketch file.

包含路径包括草图的目录、目标目录 (/hardware/core//) 和 avr 包含目录 (/hardware/tools/avr/avr/include/),以及任何库目录(在 /hardware/libraries /) 包含一个头文件,该头文件包含在主草图文件中。

This means that if you #include "ReferencedLibrary.h"from your main sketch file, this causes that file's librariesdirectory to get added to the include path for other libraries to include. A bit of a hack but it does work on my Mac.

这意味着如果你#include "ReferencedLibrary.h"从你的主草图文件,这会导致该文件的libraries目录被添加到包含其他库的包含路径中。有点小技巧,但它确实适用于我的 Mac。

回答by Tim Egbert

Using the Arduino environement, as I understand it, you cannot access your own library from another of your own libraries. There is no way to add paths, so there is simply no way for the compiler to find the code. That makes it hard to write libraries that use code in another of your libraries. My web research indicates this has been a problem for years but to my knowledge has not been solved. I suspect there are difficulties in the implementation details or perhaps a desire to keep the system simple at the expense of capability.

使用 Arduino 环境,据我所知,您无法从您自己的另一个库访问您自己的库。无法添加路径,因此编译器根本无法找到代码。这使得编写使用另一个库中代码的库变得困难。我的网络研究表明这已经是一个多年的问题,但据我所知还没有解决。我怀疑在实现细节方面存在困难,或者可能希望以牺牲能力为代价保持系统简单。

Of course, you can always cut and paste code into each new library, but that's exceedingly sub-optimal. You can also write one huge library with all of your code in one pair of .h and .cpp files. That's also not very satisfactory, but I've done it on occasion.

当然,您始终可以将代码剪切并粘贴到每个新库中,但这非常不理想。您还可以在一对 .h 和 .cpp 文件中编写一个包含所有代码的大型库。这也不是很令人满意,但我偶尔会这样做。

There is a work around, however, for using standard Arduino libraries in your own library that you're placing in your sketchbook/libraries directory. Since sketches include paths to the standard library locations, and link the standard library code, you can include the header file for the standard library of interest in your sketch. Below that, also in your sketch, include your own library header file. The standard library will then become available to your library as well as to your sketch.

但是,有一种解决方法可以在您自己的库中使用标准的 Arduino 库,您将其放置在您的 Sketchbook/libraries 目录中。由于草图包含标准库位置的路径并链接标准库代码,因此您可以在草图中包含感兴趣的标准库的头文件。在此之下,也在您的草图中,包括您自己的库头文件。然后标准库将可用于您的库以及您的草图。

回答by Pekka Lehtikoski

Not recommended method: It is possible to add basically any external library code to Arduino IDE build by knifing boards.txt file. Headers in c/cpp flags and libraries in ld flags. This may be useful for library dev using external tools (cmake/QT creator for me today).

不推荐的方法:基本上可以通过修改boards.txt文件将任何外部库代码添加到Arduino IDE构建中。c/cpp 标志中的标头和 ld 标志中的库。这对于使用外部工具的库开发可能很有用(今天对我来说是 cmake/QT 创建者)。

I modified /home/pekka/arduino-1.8.5/hardware/teensy/avr/boards.txt by adding "/coderoot" to gcc include path and E_OS_arduino define, modified lines below:

我修改了/home/pekka/arduino-1.8.5/hardware/teensy/avr/boards.txt,将“/coderoot”添加到gcc包含路径和E_OS_arduino定义,修改如下:

teensy36.build.flags.cpp=-fno-exceptions -felide-constructors -std=gnu++14 -Wno-error=narrowing -fno-rtti -I/coderoot -DE_OS_arduino

teensy36.build.flags.cpp=-fno-exceptions -felide-constructors -std=gnu++14 -Wno-error=narrowing -fno-rtti -I/coderoot -DE_OS_arduino

teensy36.build.flags.c=-I/coderoot -DE_OS_arduino

teensy36.build.flags.c=-I/coderoot -DE_OS_arduino