微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何强制 Arduino 库编译子文件夹中的代码?

如何解决如何强制 Arduino 库编译子文件夹中的代码?

我正在为 Arduino(以及可能的其他平台)编写一个 BLE 库,它需要基于底层硬件的代码版本略有不同。

我想做的是将特定于平台的代码放在一个名为“Variants”的子文件夹中,然后在主头文件中使用 #IFDEFS 指向它需要的文件,但无论我做什么,我似乎无法让编译器“找到”子文件夹中的任何代码。我在 Atom 上使用 Platformio 作为我的 IDE,但我怀疑这是 Arduino,而不是 IDE 问题。

/BleStar.h

// Header file BleStar.h - the main Arduino code does a #include "BleStar.h" which works fine
#ifndef BleStar_h
#define BleStar_h

if defined(BLE_DEVICE_ADAFRUIT_NRF52)
   #include "Bluefruit.h"                 // The Adafruit Bluefruit library for NRF52 chipsets
   #include "Variants/AdafruitNRF52.h"    // The NRF52 specific code that I wrote
#elif define(BLE_DEVICE_ADAFRUIT_NRF51)
   #include "Adafruit_BluefruitLE_SPI.h"  // The Adafruit Bluefruit library for NRF51 chipsets
   #include "Adafruit_BLE.h"
   #include "Variants/AdafruitNRF51.h"    // The NRF51 specific code that I wrote
#endif

class BleStar {
public:
   ...
   // Declarations here for the hardware specific code that shows up in "Variants/AdafruitNRF51.h" etc.
   int write(uint8_t u);
   uint8_t read();
   boolean available();
   ...
private:
};

/Variants/AdafruitNRF52.h

#ifndef AdafruitNRF52_h
#define AdafruitNRF52_h

#include "BleStar.h"
// nothing in here - declarations are in BleStar.h as part of the class declaration,and AFAIK you can't split a class declaration across multiple headers

#endif

/Variants/AdafruitNRF52.cpp

#ifndef AdafruitNRF52_cpp
#define AdafruitNRF52_cpp

#include "BleStar.h"      // shouldn't hurt to repeat it
#include "AdafruitNRF52.h"

int BleStar::write(uint8_t u) {
   //nrf52 specific code
}

uint8_t BleStar::read() {
   // more nrf52 specific code
}
// etc.


#endif

什么不起作用:

  • 以上。当 AdafruitNRF52.h 位于子目录中时,在 BleStar.h 中使用 #include AdafruitNRF52.h。我收到了一个未定义的对 `BleStar::write(uint8_t)' 错误的引用,因此编译器无法“看到”.cpp 文件
  • 在 BleStar.h 底部使用“#include AdafruitNRF52.cpp”(即在所有声明之后)并完全注释掉 AdafruitNRF52.h。尽管 Adafruit.cpp 有头文件保护(是的,我知道你不应该包含 .cpp 文件,但我正在尝试)

什么有效:

  • 将 AdafruitNRF52.cpp 放在由 IF 定义的主库目录中 (BLE_DEVICE_ADAFRUIT_NRF52) /// #endifs。它可以工作,但是当我添加更多硬件类型时,主目录会变得非常拥挤
  • 将单独的类放在具有 .h 和 .cpp 文件的子文件夹中。他们编译得很好。但是,如果可以避免,我不想为此创建子类。

有人知道如何强制编译库子文件夹中的代码吗?我被卡住了

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。