linux c 调用其他程序,C 程序与 C++ 程序之间的相互调用

因为 C 编译器编译函数时不带参数的类型信息,只包含函数的符号名字。如 void foo( int x ) , C 编译器会将此函数编译成类似 _foo 的符号,C 链接器只要找到了调用函数的符号,就会认为链接成功。而 C++ 编译器为了实现函数重载,会在编译时带上函数的参数信息。如它可以把上面的函数编译成类似于 _foo_int 这样的符号。

所以在 C++ 与 C 相互调用时,要用 extern "C" 加以声明。

下面用两个示例实现它们之间的相互调用(没有优化代码,只是起示例作用)。

C++ 调用 C 函数

/* cfile.c */

#include

void C_Func()

{

puts("Hello, I'm C_Func");

}

/* cppfile.cpp */

#include

extern "C" void C_Func(void);

int main()

{

C_Func();

system("pause");

return 0;

}

C 调用 C++ 内类的成员函数

/* cppfile.cpp */

#include

#include "cpphfile.h"

using namespace std;

void Myclass::print()

{

cout << "Hello, I'm print function." << endl;

}

/* cpphfile.h */

#ifndef _CPPCLASS_H__

#define _CPPCLASS_H__

class Myclass{

public:

void print();

};

#endif

/* wrapper.cpp 这个文件实现调用 C++ 的接口*/

#include "cpphfile.h"

extern "C" {

struct wrapper_class{

Myclass w_class;

};

struct wrapper_class* get_object()

{

return new struct wrapper_class;

}

void call_cpp_print(struct wrapper_class* p)

{

p->w_class.print();

}

}

/* cfile.c */

#include

struct wrapper_class *test;

int main()

{

/* 创建对象 */

test = get_object();

/* 调用 C++ 函数 */

call_cpp_print(test);

system("pause");

}

小总结

extern "C" 应该只能在 CPP 文件或被引入到 CPP 中的头文件内声明。

通常应该这样使用 extern "C":

#ifdef __cplusplus

extern "C" {

#endif

/* your text */

#ifdef __cplusplus

}

#endif

C++ 编译器中已经定义了 __cplusplus 这个宏。

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值