C++与C中的函数互相调用的处理

来源:互联网 发布:淘宝卖家刷等级 编辑:程序博客网 时间:2024/06/05 19:50

一、C++中调用C函数

C++和C是两种完全不同的编译链接处理方式,如果直接在C++里面调用C函数,会找不到函数体,报链接错误。要解决这个问题,就要在 C++文件里面显示声明一下哪些函数是C写的,要用C的方式来处理

首先引用头文件前需要加上 extern “C”,如果引用多个,那么就如下所示:

[cpp] view plaincopy
  1. extern “C”  
  2. {  
  3. #include “s.h”  
  4. #include “t.h”  
  5. #include “g.h”  
  6. #include “j.h”  
  7. };  

然后在调用这些函数之前,需要将函数也全部声明一遍:

[cpp] view plaincopy
  1. extern “C”  
  2. {  
  3. extern void A_app(int);  
  4. extern void B_app(int);  
  5. extern void C_app(int);  
  6. extern void D_app(int);  
  7. };  

C++程序中调用被c编译器编译后的函数,为什么要加extern "C"?

C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。假设某个C 函数的声明如下:

[cpp] view plaincopy
  1. void foo(int x, int y);  

该函数被C 编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字用来支持函数重载和类型安全连接。由于编译后的名字不同,C++程序不能直接调用C 函数。C++提供了一个C 连接交换指定符号extern“C”来解决这个问题。例如:

[cpp] view plaincopy
  1. extern “C”  
  2. {  
  3. void foo(int x, int y);  
  4. // 其它函数  
  5. }  
  6. 或者写成  
  7. extern “C”  
  8. {  
  9. #include “myheader.h”  
  10. // 其它C 头文件  
  11. }  

这就告诉C++编译译器,函数 foo 是个C 连接,应该到库中找名字_foo 而不是找_foo_int_int。C++编译器开发商已经对C 标准库的头文件作了extern“C”处理,所以我们可以用#include 直接引用这些头文件。

二、C中调用C++函数

调用C++函数库,一般不能直接调用,需要将C++库转换成C接口输出,方可以使用C调用;
 C++函数声明为``extern "C"''(在你的 C++代码里做这个声明),然后调用它(在你的C或者 C++ 代码里调用)。例如:
[cpp] view plaincopy
  1. // C++ code:  
  2. extern "C" void f(int);   
  3. void f(int i)  
  4. {  
  5. // ...  
  6. }   
   
然后,你可以这样使用 f()
[cpp] view plaincopy
  1. /* C code: */  
  2. void f(int);   
  3. void cc(int i)  
  4. {  
  5.     f(i);  
  6.     /* ... */  
  7. }  
当然,这招只适用于非成员函数。如果你想要在 C里调用成员函数(包括虚函数),则需要提供一个简单的包装(wrapper)。例如:
[html] view plaincopy
  1. // C++ code:  
  2. class C  
  3. {  
  4.     // ...  
  5.     virtual double f(int);  
  6. };   
  7. extern "C" double call_C_f(C* p, int i) // wrapper function  
  8. {  
  9.     return p->f(i);  
  10. }  
    
然后你可以这样调用C::f():
[cpp] view plaincopy
  1. /* C code: */  
  2. double call_C_f(struct C* p, int i);   
  3. void ccc(struct C* p, int i)  
  4. {  
  5.     double d = call_C_f(p,i);  
  6.     /* ... */  
  7. }   
如果你想在C里调用重载函数,则需提供不同名字的包装,这样才能被C代码调用,例如:
[cpp] view plaincopy
  1. // C++ code:  
  2. void f(int);  
  3. void f(double);   
  4. extern "C" void f_i(int i) { f(i); }  
  5. extern "C" void f_d(double d) { f(d); }  
然后,你可以这样使用每个重载的 f():
[cpp] view plaincopy
  1. /* C code: */  
  2. void f_i(int);  
  3. void f_d(double);   
  4. void cccc(int i,double d)  
  5. {  
  6.     f_i(i);  
  7.     f_d(d);  
  8.     /* ... */  
  9. }   

注意,这些技巧也适用于在 C里调用 C++ 类库,即你不能修改C++头文件。
0 0
原创粉丝点击