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

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

2014-06-18 16:08 387 查看
一、C++中调用C函数

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

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

[cpp]
view plaincopyprint?

extern “C”  
{  
#include “s.h”   
#include “t.h”   
#include “g.h”   
#include “j.h”   
};  

extern “C”
{
#include “s.h”
#include “t.h”
#include “g.h”
#include “j.h”
};

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

[cpp]
view plaincopyprint?

extern “C”  
{  
extern void A_app(int);  
extern void B_app(int);  
extern void C_app(int);  
extern void D_app(int);  
};  

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 函数的声明如下:

[cpp]
view plaincopyprint?

void foo(int x, int y);  

void foo(int x, int y);

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

[cpp]
view plaincopyprint?

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

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++ 代码里调用)。例如:

[cpp]
view plaincopyprint?

// C++ code:   
extern "C" void f(int);   
void f(int i)  
{  
// ...   
}   

// C++ code:
extern "C" void f(int);
void f(int i)
{
// ...
}

   
然后,你可以这样使用 f():

[cpp]
view plaincopyprint?

/* C code: */  
void f(int);   
void cc(int i)  
{  
    f(i);  
    /* ... */  
}  

/* C code: */
void f(int);
void cc(int i)
{
f(i);
/* ... */
}

当然,这招只适用于非成员函数。如果你想要在 C里调用成员函数(包括虚函数),则需要提供一个简单的包装(wrapper)。例如:

[html]
view plaincopyprint?

// 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++ 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():

[cpp]
view plaincopyprint?

/* 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 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代码调用,例如:

[cpp]
view plaincopyprint?

// 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); }  

// 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():

[cpp]
view plaincopyprint?

/* C code: */  
void f_i(int);  
void f_d(double);   
void cccc(int i,double d)  
{  
    f_i(i);  
    f_d(d);  
    /* ... */  
}   

/* 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++头文件。
http://blog.csdn.net/jirongzi_cs2011/article/details/14091163
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: