动态库的生成与对.so的读取(转)
来源:互联网 发布:历史书 知乎 编辑:程序博客网 时间:2024/06/10 09:39
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就再也不能使用它的符号了。
- 动态库的生成与对.so的读取
- 动态库的生成与对.so的读取(转)
- Linux下动态库so的生成与使用
- Linux下动态库so的生成与使用
- Linux下动态链接库so的生成与使用
- Linux下动态库so的生成与使用
- Linux动态库.a与动态库.so的生成与区别、以及.so库文件的封装与使用
- Android生成动态库so的方法
- 【转】gcc动态链接库*.so文件的生成与使用方法
- 【转】gcc动态链接库*.so文件的生成与使用方法
- 通过读取properties文件动态生成对数据库的连接
- linux so 库的生成与调用
- Linux下Eclipse进行C++编程动态库so的生成与使用
- .so 的生成与使用
- linux-C直接调用SO动态库和生成SO动态库的函数
- unix的AIX,xlc生成动态库后为什么还要用ln -s 对*.so动态库生成软连接为*.a
- Linux 下 动态链接库 .so 的生成和使用
- linux下.so动态库的生成、编译、调用
- [转] RPM打包技术与典型SPEC文件分析
- 帮忙
- POJ 3264 Balanced Lineup(RMQ 线段树)
- sigsuspend 将重设signal mask和等待合成原子操作
- Oracle 10g客户端 安装(配图)
- 动态库的生成与对.so的读取(转)
- 字体安装
- 探讨Spring与Hibernate的整合所遇到的管理Session的问题
- 简述Session与Cookie区别
- js错误导致session丢失
- 手动创建oracle 10g数据库的命令语法
- Flex HTTPService 跨域访问
- 主题:Action的配置属性及其含义
- Linux系统启动脚本分析之rc