您的位置:首页 > 其它

extern "C"

2013-03-23 14:14 148 查看
C++支持函数重载,而C不支持,两者的编译规则也不一样。在c++中,为了支持重载机制,在编译生成的汇编码中,要对函数的名字进行一些处理,加入函数的参数信息等等,从而区分同名函数.而在C中不会加入其他的信息.因此,函数被C++编译后在符号库中的名字与C语言的不同。例如,假设某个函数的原型为:void
foo( int x, int y ); 该函数被C编译器编译后在符号库中的名字可能为_foo,而C++编译器则会产生像_foo_int_int之类的名字(不同的编译器可能生成的名字不同,但是都采用了相同的机制,生成的新名字称为“mangled name”)。_foo_int_int这样的名字包含了函数名、函数参数数量及类型信息,C++就是靠这种机制来实现函数重载的。使用extern
"C"能够实现C与C++的相互调用问题。

extern "C"是C++关键字(不是C的关键字),表示连接声明(linkage declaration)。extern "C"包含双重含义,从字面上即可知:

(1)被extern "C"修饰的变量和函数是extern类型的

(2)被extern "C"修饰的变量和函数是按照C语言方式编译和连接的,C++连接器寻找变量和函数的符号时需要按照C的方式寻找。

下面以例子说明,如何在C++中使用C的函数,或者在C中使用C++的函数。

一://C++引用C函数的例子(C++调用C,extern "C" 的作用是:让C++连接器找调用函数的符号时采用C的方式 如)

//test.h

void mytest();

//test.c

#include <stdio.h>

void mytest()

{

printf("mytest in .c file ok\n");

}

//main.cpp

extern "C"

{

void mytest();

}

int main()

{

mytest();

return 0;

}

或者:

在后在main.cpp中extern "C"

{

#include “test.h”

}

extern "C"是告诉C++编译器件括号里的东东是按照C的obj文件格式编译的,要连接的话按照C的命名规则去找.

二://在C中引用C++函数

C调用C++,使用extern "C"则是告诉编译器把cpp文件中extern "C"定义的函数依照C的方式来编译封装接口,当然接口函数里面的C++语法还是按C++方式编译。

在C中引用C++语言中的函数和变量时,C++的函数或变量要声明在extern "C"{}里,但是在C语言中不能使用extern "C",否则编译出错。(出现错误: error C2059: syntax error : 'string',这个错误在网上找了很久,国内网站没有搜到直接说明原因的,原因是extern
"C"是C++中的关键词,不是C的,所有会出错。那怎么用?看本文,或者搜extern "C"!)

//test.cpp

#include <stdio.h>

extern "C"

{void mytest();}

void mytest()

{

printf("mytest in .cpp file ok\n");

}

//main.c

extern void mytest();

int main()

{

mytest();

return 0;

}

三。//综合使用

一般我们都将函数声明放在头文件,当我们的函数有可能被C或C++使用时,我们无法确定被谁调用,使得不能确定是否要将函数声明在extern "C"里,所以,我们可以添加

#ifdef __cplusplus

extern "C"

{

#endif

//函数声明

#ifdef __cplusplus

}

#endif

这样的话这个文件无论是被C或C++调用都可以,不会出现上面的那个错误:error C2059: syntax error : 'string'。

如果我们注意到,很多头文件都有这样的用法,比如string.h,等等。

//test.h

#ifdef __cplusplus

#include <iostream>

using namespace std;

extern "C"

{

#endif

void mytest();

#ifdef __cplusplus

}

#endif

这样,可以将mytest()的实现放在。c或者。cpp文件中,可以在。c或者。cpp文件中include "test.h"后使用头文件里面的函数,而不会出现编译错误。

//test.c

#include "test.h"

void mytest()

{

#ifdef __cplusplus

cout 《 "cout mytest extern ok " 《 endl;

#else

printf("printf mytest extern ok n");

#endif

}

//main.cpp

#include "test.h"

int main()

{

mytest();

return 0;

}

关于C++引用C函数和变量的例子还有一个

两个文件:

c文件:C.c

***********************************************

int external="5"; //全局变量,缺省为extern。

int func() //全局函数,缺省为extern。

{

return external;

}

***********************************************

cpp文件:CPP.cpp

***********************************************

#include "iostream"

using namespace std;

#ifdef __cplusplus

extern "C"

{

#endif

extern int external; //告诉编译器extern是在别的文件中定义的int,这里并不会为其分配存储空间。

extern int func(); //虽然这两个都是在extern "C"的{}里,但是仍然要显式指定extern,否则报错。

#ifdef __cplusplus //不仅仅是函数,变量也要放在extern "C"中。

}

#endif

void main(void)

{

cout《"the value of external in c file is: "《EXTERNAL《ENDL;

external=10;

cout《"after modified in cpp is : "《FUNC()《ENDL;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: