设为首页 加入收藏

TOP

Linux C调用C 库(用C封装C 接口)
2014-11-24 03:29:30 来源: 作者: 【 】 浏览:0
Tags:Linux 调用 封装 接口

C一般不能直接调用C++函数库,需要将C++库封装成C接口后,才可以使用C调用。
  下面举例,说明一个封装策略:


//code in add.cxx
#include "add.h"
int sample::method()
{
cout<<"method is called!\n";
}
//code in add.h
#include
using namespace std;
class sample
{
public:
int method();
};


  将上面的两个文件生成动态库libadd.so放到 /usr/lib目录下,编译命令如下:


  sudo g++ -fpic -shared -g -o /usr/lib/libadd.so add.cxx -I ./


  由于在C中不能识别类,所以要将上面类的成员函数,要封装成C接口函数才能被调用。下面进行封装,将输出接口转换成C接口。


//code in mylib.cxx
#include "add.h"
#ifndef _cplusplus
#define _cplusplus
#include "mylib.h"
#endif


int myfunc()
{
sample ss;
ss.method();
return 0;
}
//code in mylib.h
#ifdef _cplusplus
extern "C"
{
#endif


int myfunc();


#ifdef _cplusplus
}
#endif


  在linux下,gcc编译器并没用变量_cplusplus来区分是C代码还是C++代码(没有宏定义),如果使用gcc编译器,这里我们可以自己定义一个变量_cplusplus用于区分C和C++代码,所以在mylib.cxx中定义了一个变量_cplusplus用于识别是否需要“extern "C"”将函数接口封装成C接口。但是如果使用g++编译器则不需要专门定义_cplusplus,编译命令如下:


  g++ -fpic -shared -g -o mylib.so mylib.cxx -la -I ./


main.c
#include
#include
#include "mylib.h"


int
main()
{
int (*dlfunc)();
void *handle; //定义一个句柄
handle = dlopen("./mylib.so", RTLD_LAZY);//获得库句柄
dlfunc = dlsym(handle, "myfunc"); //获得函数入口
(*dlfunc)();
dlclose(handle);


return 0;
}


  编译命令如下:


  gcc -o main main.c ./mylib.so -ldl


  下面就可以执行了。


  需要说明的是,由于main.c 和 mylib.cxx都需要包含mylib.h,并且要将函数myfunc封装成C接口函数输出需要“extern "C"”,而C又不识别“extern "C"”,所以需要定义_cplusplus来区别处理mylib.h中的函数myfunc。


  在main.c的main函数中直接调用myfunc()函数也能执行,这里介绍的是常规调用库函数的方法。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux下测试键盘鼠标触摸屏触摸板 下一篇Linux函数库操作相关

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·常用meta整理 | 菜鸟 (2025-12-25 01:21:52)
·SQL HAVING 子句:深 (2025-12-25 01:21:47)
·SQL CREATE INDEX 语 (2025-12-25 01:21:45)
·Shell 传递参数 (2025-12-25 00:50:45)
·Linux echo 命令 - (2025-12-25 00:50:43)