动态库的生成与对.so的读取(转)

38 篇文章 0 订阅

http://blog.csdn.net/wyl123/archive/2008/12/10/3486124.aspx

 

 

test.h

#ifndef _TEST_H

#define _TEST_H

void test_a();

void test_b();

#endif

test_a.c

#include <stdio.h>

#include "test.h"

void test_a()

{

printf("test in test_a..../n");

}

test_b.c

#include <stdio.h>

#include "test.h"

void test_b()

{

printf("test in test_b..../n");

}

test.c

#include <stdio.h>

#include "test.h"

int main()

{

test_a();

test_b();

return 0;

}

main.c

#include <stdio.h>

#include <dlfcn.h>

int main()

{

void *handle;

char *error;

void (*test_a)();

void (*test_b)();

if((handle = dlopen("./libtest.so",RTLD_LAZY)) == NULL)

{

printf("dlopen error/n");

return -1;

}

test_a = dlsym(handle,"test_a");

test_b = dlsym(handle,"test_b");

if((error = dlerror()) != NULL)

{

printf("dlsym error /n");

return -1;

}

test_a();

test_b();

dlclose(handle);

return 0;

}

编译成动态库libtest.so

gcc test_a.c test_b.c -fPIC -shared -o libtest.so

gcc -o test test.c ./libtest.so

ldd test

gcc -o main main.c -ldl

 

关于gcc参数 -fPIC参阅网页:http://fair.athost.net/main/gcc/index.html#I204

 

 

Linux中DSO函数操作
void* dlopen(const char *pathname, int mode);
该函数函 数来加载动态库,其中pathname是需要加载的库的路径名称,mode则是加载的方式,可以是三个值:RTLD_LAZY用来表示认为未定义的符号是 来自动态链接库的代码;RTLD_NOW则表示要在dlopen返回前确定所有未定义的符号,如果不能完成则失败;而RTLD_GLOBAL则意味着动态 链接库中定义的外部符号将可以由随后加载的库所使用。如果函数执行成功,其将返回动态链接库的一个句柄。
一旦对动态库进行了加载,我们则可以通过dlsym函数获取库中的函数调用以及各种定义的符号等等。其函数原型如下:
void* dlsym(void* handle, char* symbol);
其中,handle是加载的动态链接库的句柄,其通常是dlopen函数的操作结果;symbol则是需要得到的动态链接库中的符号名称。如果找不到symbol,函数将返回NULL。
在所有的操作结束后,Linux可以通过dlclose将dlopen先前打开的共享对象从当前进程断开,不过只有动态链接库的使用记数为0的时候该共享对象才会真真被卸载。dlclose的函数原型如下:
int dlclose(void* handle);
一旦使用dlclose关闭了对象,dlsym就再也不能使用它的符号了。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值