C调用C++的函数和变量时,C++的头文件要加extern “C”,且.C文件不能直接包含extern “C”的头文件,要另外用extern 声明所要用的C++中的函数。
#ifdef _cplusplus
extern "C" {
#endif
......
#ifdef _cplusplus
}
#endif
C++调用C的函数和变量时,C++文件可以包含C的头文件,但要写成如下形式:
extern “C” {
#include "c_file.h"
}
用extern “C” 修饰的 函数,则该函数以 C的方式编译和链接。
C++编译函数的方式是将int foo(int a, int b)编译成foo_int_int形式的函数,这也是C++实现函数重载的原因。而C的编译方式则不是这样的。