关于“#ifdef __cplusplus” 和 " extern "C" " 看一些程序的时候老是有 “#ifdef __cplusplus extern "C" { #endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefined Macros __cplusplus Defined for C++ programs only
意思是说,如果是 C++程序,就使用 extern "C"{ 而这个东东,是指在下面的函数不使用的 C++的名字修饰,而是用 C的 The following code shows a header file which can be used by C and C++ client applications: // MyCFuncs
h #ifdef __cplusplus extern "C" { //only need to export C interface if // used by C++ source code #endif __declspec( dllimport ) void MyCFunc(); __declspec( dllimport ) void AnotherCFunc(); #ifdef __cplusplus } #endif 当我们想从 C++中调用 C的库时,(注,驱动是用 C写的,连 new、delete也不能用,郁闷)不能仅仅说明 一个外部函数,因为调用 C函数的编译代码和调用C++函数的编译代码是不同的
如果你仅说明一个外部函数, C++编译器假定它是 C++的函数编译成功了,但当你连接时会发现很可爱的错误
解决的方法就是指定它为 C函数: extern "c" 函数描述 指定一群函数的话: extern "C"{ n 个函数描述 } 如果想 C和 C++混用