C和C++混合编程注意事项
Extern “C”是由C++提供的一个连接交换指定符号,用于告诉C++这段代码是C函数。这是因为C++编译后库中函数名会变得很长,与C生成的不一致,造成C++不能直接调用C函数,加上extren “c”后,C++就能直接调用C函数了。
Extern “C”主要使用正规DLL函数的引用和导出 和 在C++包含C函数或C头文件时使用。使用时在前面加上extern “c” 关键字即可。extern "C"是C++的声明,C当然不能识别——因为C的函数本来就是extern "C"的,没有必要画蛇添足。
demofunc.c:
int DemoFunc(int a)
{
return a;
}
-------------
demo.h:
extern "C" int DemoFunc(int);
demo.cpp:
#i nclude "demo.h"
#i nclude "iostream.h"
void main()
{
cout << DemoFunc(5) << endl;
}