linux动态链接库中函数的运行时加载
2013-07-16 20:21
435 查看
一. 相关函数:dlopen(打开共享库),dlsym(查找符号),dlerror(错误信息),dlclose(关闭共享库) 1. dlopen()
3.编译运行
原型:void* dlopen(const char *filename, int flag);2. dlsym() 3. dlerror() 4. dlclose()二. 源码实例 1. 动态库文件:lib.c,lib.h
#include <stdio.h> void output(int index) { printf("Printing from lib.so. Called by program %d\n", index); }
#file:lib.h #ifndef LIB_H #define LIB_H void output(int index); #endif2. main.c
#include <stdio.h> #include <dlfcn.h> int main() { void *handle; void (*fun)(int); char* error; handle = dlopen("/home/hotpatch/dynamic_lib/lib.so", RTLD_NOW); if(NULL == handle) { printf("Open library error.error:%s\n",dlerror()); return -1; } fun = dlsym(handle,"output"); if(NULL != (error = dlerror())) { printf("Symbol output is not found:%s\n",error); goto exit_runso; } fun(10); printf("Function address is: 0x%016x\n",fun); exit_runso: dlclose(handle); return 0; }
3.编译运行
编译: gcc -fPIC -shared -o lib.so lib.c gcc -o test main.c -ldl 运行: Printing from lib.so. Called by program 10 Function address is: 0x00000000e67ba604
相关文章推荐
- Intel平台下linux中 ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- linux中ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- Intel平台下linux中 ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- Intel平台下linux中ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- Intel平台下linux中ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- Linux动态链接之五:运行时显式加载共享文件.so
- 动态链接库DLL的加载:隐式加载(载入时加载)和显式加载(运行时加载)
- Intel平台下Linux中 ELF文件动态链接的加载、解析及实例分析(一): 加载
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析
- [LinuxC]加载动态链接库so程序简单实例
- 动态符号链接的细节 与 linux程序的加载过程
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析
- Intel平台下Linux中 ELF文件动态链接的加载、解析及实例分析(一): 加载
- linux 程序运行时 指定动态链接库位置 so library
- IBM的LPI复习资料之LPI101-Topic102:Linux安装和包管理(3)管理动态链接库(查找和加载程序需要的动态链接库文件)
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析(一): 加载
- Linux下库函数动态链接过程分析
- 如何将arm-linux-gcc编译的动态链接helloworld在Android上运行
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析
- Intel平台下Linux中ELF文件动态链接的加载、解析及实例分析(一):