Arduino IDE无法识别.c文件是.cpp

时间:2016-11-14 02:28:06

标签: c++ arduino arduino-ide

我正在为Arduino IDE的特定主板制作库。图书馆工作得很好,现在我退后一步来添加OO。该库是.c和.cpp文件的混合。我知道为了添加类,我只需要使用.cpp。

这是LED.h文件。

https://gist.github.com/SaraJo/182220fda82cbe30255fe95f59d4a6b4

这是LED.cpp文件。

https://gist.github.com/SaraJo/1b3d6967d7bc2ef2e70d79025b755eb9

我得到的错误是:

In file included from /Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/Arduino.h:54:0,
                 from /Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/ble-nrf51822-master/source/main.c:49:
/Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/LED.h:12:1: error: unknown type name 'class'
 class LED {
 ^
/Users/sarachipps/Library/Arduino15/packages/Jewelbots/hardware/nRF51822/1.0.0/cores/JWB_nRF51822/LED.h:12:11: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
 class LED {
           ^
exit status 1
Error compiling for board JWB nRF51822(V1.0 32KB).

我猜Arduino将.cpp文件视为.c,是否需要设置编译器标志?谢谢。

2 个答案:

答案 0 :(得分:7)

因此,问题是main.c的C编译器不理解C ++头文件LED.h中的“class”关键字。您可以将main.c更改为main.cpp并查看是否有效吗?

(您可能还需要添加

#ifdef __cplusplus
extern "C" {
#endif

位于顶部,

#ifdef __cplusplus
}
#endif

位于main.h文件的底部(或者可能是main.cpp文件?),这样C ++就不会试图破坏某些函数的名称,以便链接器可以找到它们...

答案 1 :(得分:2)

您不能在C文件的头文件中包含C ++声明。如果需要在同一个头文件中混合C和C ++声明,请在

中包装C ++
#ifdef __cplusplus
class MyClass {
  // ...
};
#endif