extern C(C++与C实现相互函数的调用)

来源:互联网 发布:淘宝旺旺怎么下载 编辑:程序博客网 时间:2024/04/29 09:44

(1) C++中调用C的函数

#ifdef _cplusplus  extern "C" {#endif  void fun(int);  void fun1(int);#ifdef _cplusplus  }#endif
(2)C中调用C++函数

A. C中调用C++非成员函数

在C++中函数声明前加入 extern "C"

B. C中调用C++成员函数

如果你想要在 C 里调用成员函数(包括虚函数),则需要提供一个简单的包装(wrapper)。

//C++ class m{public:int data;virtual int fun(int);};extern "C" int call_C_fun(m* p,int i){return p->fun(i);}

C. C中调用C++中的重载函数

如果你想在 C 里调用重载函数,则必须提供不同名字的包装,这样才能被 C 代码调用。


参考来源: http://blog.csdn.net/imcainiao11/article/details/7369447

0 0
原创粉丝点击