简体   繁体   English

是否可以使用 Arduino IDE 包含来自另一个库的库?

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

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.我可以从我的 ~/Documents/Arduino 目录中的草图中成功#include <ReferencedLibrary.h> Am I missing something or is this a limitation of the Arduino IDE/makefile?我是否遗漏了什么或者这是 Arduino IDE/makefile 的限制? Is there a workaround?有解决方法吗?

I have been able to include a library in another Arduino library by using a relative path.通过使用相对路径,我已经能够在另一个 Arduino 库中包含一个库。 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:例如,要将 AbstractSwitch 库包含到 DigitalSwitch 库中,假设这两个库都位于 Arduino 标准库文件夹中各自独立的文件夹中,您可以使用以下包含语句:

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

In other words, your include statement should read:换句话说,您的 include 语句应为:

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

The documentation here https://github.com/arduino/Arduino/wiki/Build-Process states: 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 /) 其中包含一个 header 文件,该文件包含在主草图文件中。

This means that if you #include "ReferencedLibrary.h" from your main sketch file, this causes that file's libraries directory to get added to the include path for other libraries to include.这意味着如果您从主草图文件中#include "ReferencedLibrary.h" ,这会导致该文件的libraries目录被添加到包含路径以供其他库包含。 A bit of a hack but it does work on my Mac.有点破解,但它确实适用于我的 Mac。

This issue was solved in the Arduino 1.6.6 release.此问题已在 Arduino 1.6.6 版本中得到解决。 The release notes of 1.6.6 mention that library to library dependencies have been fixed. 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 或更高版本将解决您的问题。

Using the Arduino environement, as I understand it, you cannot access your own library from another of your own libraries.据我了解,使用 Arduino 环境,您无法从您自己的另一个库访问您自己的库。 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.我的 web 研究表明这多年来一直是一个问题,但据我所知尚未解决。 I suspect there are difficulties in the implementation details or perhaps a desire to keep the system simple at the expense of capability.我怀疑实现细节存在困难,或者可能是希望以牺牲功能为代价来保持系统简单。

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.您还可以在一对 .h 和 .cpp 文件中编写一个包含所有代码的大型库。 That's also not very satisfactory, but I've done it on occasion.这也不是很令人满意,但我偶尔会这样做。

There is a work around, however, for using standard Arduino libraries in your own library that you're placing in your sketchbook/libraries directory.但是,有一种解决方法可以在您自己的库中使用标准 Arduino 库,您将其放置在您的草图簿/库目录中。 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.由于草图包含标准库位置的路径,并链接标准库代码,因此您可以在草图中包含感兴趣的标准库的 header 文件。 Below that, also in your sketch, include your own library header file.在此之下,也在您的草图中,包括您自己的库 header 文件。 The standard library will then become available to your library as well as to your sketch.然后,标准库将可用于您的库以及您的草图。

Not recommended method: It is possible to add basically any external library code to Arduino IDE build by knifing boards.txt file.不推荐的方法:基本上可以将任何外部库代码添加到由 knifing board.txt 文件构建的 Arduino IDE 文件中。 Headers in c/cpp flags and libraries in ld flags. c/cpp 标志中的标头和 ld 标志中的库。 This may be useful for library dev using external tools (cmake/QT creator for me today).这可能对使用外部工具的库开发人员有用(今天我的 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM