因为 C 和 C++ 对于变量的处理是不同的,C 中,编译器在变量和函数前加一个下化线,而 C++ 的处理复杂的多。C 中定义的变量要在 C++ 中使用,你应该使用 extern "C" 声明。例如: int a;//.c 中extern "C" int a; // .cpp 中。 如果在头文件(.h)中声明,你可以写一个适合 C 和 C++ 的声明:#ifdef __cplusplus extern "C"{ #endif int func();// 函数声明; //... extern int a;//变量声明 #ifdef __cplusplus} #endif
我在 .c 文件里定义了 unsigned short b5_out;
在 .cpp 里用到了这个变量:
extern unsigned short b5_out;
然后是对这个变量的使用。
为什么编译的时候说:error LNK2001: unresolved external symbol "unsigned short * b5_out" (?b5_out@@3PAGA)???
.c 文件实际上是一个变量的定义,不过这个变量太长,所以放在一个文件里了。
并且没有对这个文件进行预处理(precompiled headers)
这样就可以了!extern "C" unsigned short b5_out;
extern "C" 声明。例如:
int a;//.c 中extern "C" int a; // .cpp 中。
如果在头文件(.h)中声明,你可以写一个适合 C 和 C++ 的声明:#ifdef __cplusplus
extern "C"{
#endif int func();// 函数声明; //...
extern int a;//变量声明
#ifdef __cplusplus}
#endif