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

前端 未结 5 853
栀梦
栀梦 2020-12-17 10:06

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 directo

相关标签:
5条回答
  • 2020-12-17 10:47

    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).

    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:

    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

    0 讨论(0)
  • 2020-12-17 10:49

    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.

    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.

    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.

    0 讨论(0)
  • 2020-12-17 11:01

    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.

    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

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

    0 讨论(0)
  • 2020-12-17 11:06

    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:

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

    In other words, your include statement should read:

    #include "../LibraryFolder/LibraryHeaderFile.h"
    
    0 讨论(0)
  • 2020-12-17 11:09

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

    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.

    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. A bit of a hack but it does work on my Mac.

    0 讨论(0)
提交回复
热议问题