您的位置:首页 > 编程语言 > C语言/C++

extern "C"实现C与C++混合编程(笔记)

2014-02-18 21:57 417 查看
#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)
改编后的方法名加入了参数和返回值类型。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  extern C __cplusplus C++
相关文章推荐