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

函数声明

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、C++中调用C函数

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

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

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

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

  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. };  
extern “C”
{
extern void A_app(int);
extern void B_app(int);
extern void C_app(int);
extern void D_app(int);
};

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

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

  1. void foo(int x, int y);  
void foo(int x, int y);

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

  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. }  
extern “C”
{
void foo(int x, int y);
// 其它函数
}
或者写成
extern “C”
{
#include “myheader.h”
// 其它C 头文件
}

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

二、C中调用C++函数

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

注意,这些技巧也适用于在 C里调用 C++ 类库,即你不能修改C++头文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值