linux动态链接库的创建与使用

这两天要做一个linux动态链接库.so文件,开始我选择用C++编写linux动态链接库,但在调用该链接库运行时会提示symbol找不到,如果用C语言编写动态链接库运行时就可以得到正确的结果。开始还以为创建linux动态链接库so文件不能用c++,后来终于找到答案,使用C++时要使用extern "C"。

使用linux动态链接库:
在Unix上存在两种库:动态库(.so)和静态库(.a),采用动态库(共享对象)技术可以方便程序间共享,节省程序占有空间,增加程序的可扩展性和灵活性。
操作动态库的函数在dlfcn.h文件中定义,所以要在使用动态库时include <dlfcn.h>
1. 打开动态链接库:dlopen,函数原型void *dlopen (const char *filename, int flag); dlopen用于打开指定名字(filename)的动态链接库,并返回操作句柄。比如,void *pHandle = dlopen(strSoFilePath, RTLD_LAZY);
2. 取动态对象地址:dlsym,函数原型为: void *dlsym(void *handle, char *symbol); dlsym根据动态链接库操作句柄(handle)与符号(symbol),返回符号对应的地址。注意使用这个函数不但获取函数地址,也可以获取变量地址。比如,假设我在so中定义了一个void mytest()函数,那在使用so时先申明一个函数指针:void (*pMytest)();然后使用dlsym函数将函数指针pMytest指向mytest函数,pMytest = (void (*)())dlsym(pHandle, "mytest");
3. 关闭动态链接库:dlclose,函数原型为: int dlclose (void *handle); 该函数将该.so的引用计数减一,当引用计数为0时,将它从系统中卸载。
4. 动态库错误函数:dlerror,函数原型为: const char *dlerror(void); 当动态链接库操作函数执行失败时,dlerror可以返回出错信息,返回值为NULL时表示没有错误信息。
在取到函数执行地址后,就可以在动态库的使用程序里面根据动态库提供的函数接口声明调用动态库里面的函数。在编写调用动态库的程序的makefile文件时,需要加入编译选项-ldl。

创建linux动态链接库:
从void *dlsym(void *handle, char *symbol); 的参数可以看出,该函数只传两个参数:一个指向so的handle和一个函数的symbol,所以so里面的函数应该不允许重载,否则根据一个symbol不能确定指向那个函数。为了兼容C和C++在so中定义函数时用extern "C",以下用例子说明如何创建linux动态链接库so文件。
//test.h
#ifdef __cplusplus
extern "C"
{
#endif
void mytest();
#ifdef __cplusplus
}
#endif

//test.c
#include <stdio.h>
#include "mytest.h"
void mytest()
{
printf("mytest ok in .so file\n");
}

//main.cpp
#include <dlfcn.h>
#include <stdio.h>
//#include "mytest.h"

void (*pMytest)();
bool testso()
{
int errno = -1;
char *error = NULL;
const char *strSoFilePath = "./mytest.so";
void *pHandle = dlopen(strSoFilePath, RTLD_LAZY);
if( pHandle==NULL )
{
if( (error=dlerror())!=NULL )
{
printf("dlopen %s error, errno: %d, error info: %s\n", strSoFilePath, errno, error);
return false;
}
else
{
printf("dlopen %s error, errno: %d\n", strSoFilePath, errno);
return false;
}
}

pMytest = (void (*)())dlsym(pHandle, "mytest");
if( (error=dlerror()) != NULL )
{
printf("Can't get function Initialize: %s\n", error);
return false;
}
pMytest();
if( pHandle != NULL )
dlclose(pHandle);
return true;
}

int main()
{
if( testso() )
printf("test so ok");
else
printf("test so fail");

return 0;
}



//Makefile
all : mytest.so testso

.cpp.o :
g++ -c -g -Wno-deprecated $(CFLAGS) $<

.c.o :
gcc -c -g -shared $(CFLAGS) $<

mytest.so : mytest.o
g++ mytest.o -g -shared -o mytest.so

testso : main.o
g++ main.o -rdynamic -s -g -Wl -o testso -ldl

clean :
rm -f *.o *.so testso
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值