我只有用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