Is it possible to include a library from another library using the Arduino IDE?
我正在尝试编写一个Arduino库(实际上是C ++类),该库本身引用了我在Mac的?/ Documents / Arduino / libraries目录中安装的另一个库。
在我正在编写的库的.cpp顶部,我已经尝试过
1 | #include <ReferencedLibrary.h> |
和
1 | #include"ReferencedLibrary.h" |
...两者都不起作用。 我可以从?/ Documents / Arduino目录中的草图成功
我已经能够通过使用相对路径在另一个Arduino库中包含一个库。例如,要将AbstractSwitch库包含在DigitalSwitch库中,并假设这两个库都位于Arduino标准库文件夹中各自独立的文件夹中,则可以使用以下include语句:
1 | #include"../AbstractSwitch/AbstractSwitch.h" |
换句话说,您的include语句应为:
1 | #include"../LibraryFolder/LibraryHeaderFile.h" |
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.
这意味着如果您从主草图文件中
这个问题在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
将您的版本更新到1.6.6或更高版本将解决您的问题。
据我了解,使用Arduino环境,您无法从自己的另一个库中访问自己的库。无法添加路径,因此编译器根本无法找到代码。这使得编写使用另一个库中的代码的库变得很困难。我的网络研究表明,多年来一直是一个问题,但据我所知尚未解决。我怀疑实现细节有困难,或者可能希望以牺牲功能为代价来保持系统简单。
当然,您始终可以将代码剪切并粘贴到每个新库中,但这是次优的选择。您还可以在一对.h和.cpp文件中编写一个包含所有代码的大型库。这也不是很令人满意,但是我有时会这样做。
但是,有一种变通方法,就是在您自己的库中使用标准Arduino库,然后将其放置在sketchbook / libraries目录中。由于草图包括指向标准库位置的路径并链接标准库代码,因此您可以在草图中包含感兴趣的标准库的头文件。在此之下,也在草图中,包括您自己的库头文件。然后,标准库将对您的库以及您的草图可用。
不推荐使用的方法:可以通过剪切boards.txt文件将基本上任何外部库代码添加到Arduino IDE构建中。 c / cpp标志中的标头和ld标志中的库。这对于使用外部工具(今天对我来说是cmake / QT创建者)的图书馆开发人员可能有用。
我通过在gcc包含路径和E_OS_arduino定义中添加" / coderoot"来修改/home/pekka/arduino-1.8.5/hardware/teensy/avr/boards.txt,修改行如下:
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