簡體   English   中英

Arduino IDE無法識別.c文件是.cpp

[英]Arduino IDE not recognizing that .c file is .cpp

我正在為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,是否需要設置編譯器標志? 謝謝。

因此,問題是main.c的C編譯器不理解C ++頭文件LED.h的“class”關鍵字。 你能改變main.cmain.cpp ,看看是否有效?

(您可能還需要添加

#ifdef __cplusplus
extern "C" {
#endif

在頂部,和

#ifdef __cplusplus
}
#endif

main.h文件的底部(或者也許是main.cpp文件?),這樣C ++就不會試圖main.h你的某些函數的名稱,這樣鏈接器就可以找到它們......

您不能在C文件的頭文件中包含C ++聲明。 如果需要在同一個頭文件中混合使用C和C ++聲明,請將C ++聲明包裝在其中

#ifdef __cplusplus
class MyClass {
  // ...
};
#endif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM