我只有用C編寫的代碼。主要功能是main.c
,它包括其他c文件,如#include "Flash.h"
。爲什麼我不能在main.cpp中包含C文件?
我想保持項目正常工作,但能夠添加cpp
文件。 有人告訴我,我必須要改變的主要是
main.cpp
時將產生compilation- 250個錯誤對於包括Ç文件
什麼是將主文件CPP的正確方法仍然包含C文件?
main.cpp中:
#include "Flash.h"
int main(void)
{
....
}
我已閱讀How to use C source files in a C++ project?這並沒有給我提供一個直接的解決問題的方法(我的編譯器將使用C++反正)。
編輯:
之前有人會殺了我的要求(不知道爲什麼你這麼激進的),我得到的只有3種錯誤250倍:
'->' cannot appear in a constant-expression
'&' cannot appear in a constant-expression
a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
編輯2: 下面是一些線路(運行使用SDK的某些射頻芯片): 大部分錯誤來自本節
typedef enum /*lint -save -e30 -esym(628,__INTADDR__) */
{
NRF_GPIOTE_TASKS_OUT_0 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[0]), /**< Out task 0.*/
NRF_GPIOTE_TASKS_OUT_1 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[1]), /**< Out task 1.*/
NRF_GPIOTE_TASKS_OUT_2 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[2]), /**< Out task 2.*/
NRF_GPIOTE_TASKS_OUT_3 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[3]), /**< Out task 3.*/
#if (GPIOTE_CH_NUM > 4) || defined(__SDK_DOXYGEN__)
NRF_GPIOTE_TASKS_OUT_4 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[4]), /**< Out task 4.*/
NRF_GPIOTE_TASKS_OUT_5 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[5]), /**< Out task 5.*/
NRF_GPIOTE_TASKS_OUT_6 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[6]), /**< Out task 6.*/
NRF_GPIOTE_TASKS_OUT_7 = offsetof(NRF_GPIOTE_Type, TASKS_OUT[7]), /**< Out task 7.*/
在這些線路上同樣的錯誤重複:
a cast to a type other than an integral or enumeration type cannot appear in a constant-expression
C和C++仍然是兩種截然不同的語言,您不能指望爲C編譯器編寫的頭文件用C++編譯器完美編譯。 –
爲什麼混合使用C和C++?難怪它不會編譯。您必須改用'main.c'。 – Rakete1111
你使用什麼編譯器?有些會根據擴展名(例如'.c' vs'.cpp')自動檢測語言(C vs C++)。它們確實是不同的語言,C語言程序不一定會編譯爲C++語言,因爲語言差異很大。 – CoryKramer