Linux 下C语言中调用C++动态库完整流程

1、C++中要供给C使用的函数必须经过如下处理。

#ifdef __cplusplus
extern "C"{
#endif

//这里放置C语言需要调用的函数。
int get_parameter_result(double *dataY, double *dataX,double *result);
#ifdef __cplusplus
}
#endif
#endif
2、编译C++文件生成动态库。本文只是一个示例,如果是项目,需要更换为Makefile 或CMAKE来生成动态库。

g++ -fPIC -c CouveFitter.cpp test_fit.cpp fitterInterface.cpp
g++ -shared CouveFitter.o test_fit.o fitterInterface.o -o logisio.so

3、在C语言中直接调用get_parameter_result,链接库文件跟链接C动态库文件一致。

gcc -o myfour gfour.c logisio.so

猜你喜欢

转载自blog.csdn.net/dreamliweiming/article/details/134834760