在Linux操作系统下共享库编程的方法介绍

在Linux操作系统下共享库编程的方法介绍
导读:
  一、说明
  类似Windows系统中的动态链接库,Linux中也有相应的共享库用以支持代码的复用。Windows中为*.dll,而Linux中为*.so。下面详细介绍如何创建、使用Linux的共享库。
  二、创建共享库
  在mytestso.c文件中,代码如下:
#include <stdio.h>
#include <stdlib.h>
int GetMax(int a, int b)
{
if (a >= b) return a;
 return b;
}
int GetInt(char* psztxt)
{
 if (0 == psztxt) return -1;
return atoi(psztxt);
}
  然后使用下列命令进行编译:
  gcc -fpic -shared mytestso.c -o mytestso.so
  -fpic 使输出的对象模块是按照可重定位地址方式生成的
  编译成功后,当前目录下有mytestso.so,此时已成功创建共享库mytestso.so。
  三、使用共享库
  共享库中的函数可被主程序加载并执行,但是不必编译时链接到主程序的目标文件中。主程序使用共享库中的函数时,需要事先知道所包含的函数的名称(字符串),然后根据其名称获得该函数的起始地址(函数指针),然后即可使用该函数指针使用该函数。
  在mytest.c文件中,代码如下:
#include <dlfcn.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
void* pdlhandle;
char* pszerror;

int (*GetMax)(int a, int b);
int (*GetInt)(char* psztxt);

int a, b;
char* psztxt = "1024";

// open mytestso.so
pdlhandle = dlopen("./mytestso.so", RTLD_LAZY);
pszerror = dlerror();
if (0 != pszerror)

 {
printf("%s", pszerror);
exit(1);
}

// get GetMax func
GetMax = dlsym(pdlhandle, "GetMax");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s", pszerror);
exit(1);
}

// get GetInt func
GetInt = dlsym(pdlhandle, "GetInt");
pszerror = dlerror();
if (0 != pszerror) {
printf("%s", pszerror);
exit(1);
}

// call fun
a = 200;
b = 600;
printf("max=%d", GetMax(a, b));
printf("txt=%d", GetInt(psztxt));

// close mytestso.so
dlclose(pdlhandle);
}
  然后使用如下命令进行编译:
  gcc mytest.c -ldl -o mytest
  -ldl选项,表示生成的对象模块需要使用共享库
  (1)dlopen()
  第一个参数:指定共享库的名称,将会在下面位置查找指定的共享库。
  -环境变量LD_LIBRARY_PATH列出的用分号间隔的所有目录。
  -文件/etc/ld.so.cache中找到的库的列表,用ldconfig维护。
  -目录usr/lib。
  -目录/lib。
  -当前目录。
  第二个参数:指定如何打开共享库。
  -RTLD_NOW:将共享库中的所有函数加载到内存
  -RTLD_LAZY:会推后共享库中的函数的加载操作,直到调用dlsym()时方加载某函数
  (2)dlsym()
  调用dlsym时,利用dlopen()返回的共享库的phandle以及函数名称作为参数,返回要加载函数的入口地址。
  (3)dlerror()
  该函数用于检查调用共享库的相关函数出现的错误。
  四、结束语
  本文主要阐述了Linux系统使用共享库的编程问题,并通过一个简单的实例详细说明。

本文转自
http://www.chineselinuxuniversity.net/articles/6338.shtml

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值