#ifdef __cplusplus
extern "C" {
#endif
...//代码
#ifdef __cpluscplus
};
#endif
extern "C"表示不进行名字改编。C++支持重载,所以编译器会对重载的函数进行名字改编(name managling)。
#include <iostream>
using namespace std;
void func(int a, int b){
cout<<"int fun"<<endl;
}
void func(double a, double b) {
cout<<"double fun"<<endl;
}
void func(int a, int b, int c);
int main(void)
{
func(3,4);
func(3.3,4.4);
func(3,4,5);
return 0;
}
func(3,4,5)这行报错,从控制台可以看到:
error LNK2001: 无法解析的外部符号 "void __cdecl func(int,int,int)" (?func@@YAXHHH@Z)
改编后的方法名加入了参数和返回值类型。