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

怎样编写能够被C调用的C++函数

2010-09-02 13:58 232 查看
比如你用C++编写了某个函数

/* a.hpp */

void f(int i, char c, float x);

/* a.cpp */

#include "a.hpp"

void f(int i, char c, float x)

{

 

}

 

你想用g++编译该函数 g++ -c a.cpp ,这样编译出来的a.o是无法被gcc用来链接的,由于cpp和c的调用约定和name mangling的不同,为了达到目的,我们可以这么修改a.hpp

/ * a.hpp */

#ifdef __cplusplus

extern "C" {

#endif

 

void f(int i, char c, float x);

 

#ifdef __cplusplus

}

#endif

 

 

同时在编译的时候比如关闭一些c++特有选项,比如g++ -fno-exceptions,最终编译出来的a.o就能够被gcc链接成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c c++ float gcc