linux中的显式运行时链接---dlopen、dlsym、dlerror、dlclose搞起
来源:互联网 发布:java struct 编辑:程序博客网 时间:2024/05/22 14:00
之前, 我们在linux下制作过动态库, 并卖过。 当时我们采用的是隐式链接, 也就是说, 在代码中, 我们不用去关如何打开动态库、如何链接(这些活都交给了链接器), 只需要管如何调用就行。 隐式链接的动态库被进程加载到内存后, 就一直站着这个坑, 直到进程终结。 费时费力, 浪费地盘, 而且不灵活。
于是, 一种动态加载(程序中指定加载)的方式就出现了, 这就是我们今天要说的显式运行时加载, 这个加载、卸载过程受程序员本身控制(而非链接器), 所以有了更大的自由度。 对于一些需要长时间运行的程序(如后台服务), 利用显式运行时链接链接就很有好处了, 非常便于升级so库。
废话少说, 直接撸起:
taoge_add.c的代码为:
int add(int x, int y){ return x + y;}main.c的代码为:
#include <stdio.h>#include <stdlib.h>#include <dlfcn.h>typedef int (*pFUN)(int, int); // 函数指针类型int main(){ void *pHandle = NULL; char *pError = NULL; pFUN pfun = NULL; pHandle = dlopen("./libtaoge.so", RTLD_NOW); // 打开动态链接库 if (NULL == pHandle) { printf("dlopen:%s\n", dlerror()); return -1; } pfun = dlsym(pHandle, "add"); if ((pError = dlerror()) != NULL) // 这里不要直接用pfun和NULL比较{ printf("dlsym:%s\n", pError); return -2; } printf("sum is : %d\n", (*pfun)(1, 2)); dlclose(pHandle);return 0;}
好, 撸起:
taoge@localhost Desktop> gcc -shared -fPIC taoge_add.c -o libtaoge.sotaoge@localhost Desktop> gcc main.c -o a.out -ldltaoge@localhost Desktop> ./a.out sum is : 3taoge@localhost Desktop>结果OK, 我们看看编译和链接过程:
1. 制作动态库libtaoge.so的方法和以前完全一致
2. 链接的方式不一样了, 以前需要在编译main.c时候指定libtaoge.so库, 现在是执行dl.so库(应为程序中链接的API需要它)。 其实, 本质也是一致的, 就是想办法找到libtaoge.so, 然后链接到它。
下面, 我们继续看看如果libtaoge.so不存在会怎么办?
taoge@localhost Desktop> gcc -shared -fPIC taoge_add.c -o libtaoge.sotaoge@localhost Desktop> gcc main.c -o a.out -ldltaoge@localhost Desktop> ./a.out sum is : 3taoge@localhost Desktop> rm libtaoge.so taoge@localhost Desktop> ./a.out dlopen:./libtaoge.so: cannot open shared object file: No such file or directory这个提示好眼熟 这也从侧面说明, 在编译截断, 动态库没有被编进a.out, 是运行时动态加载的。
0 0
- linux中的显式运行时链接---dlopen、dlsym、dlerror、dlclose搞起
- dlopen dlsym dlclose dlerror
- dlopen dlsym dlclose dlerror
- dlclose、dlerror、dlopen、dlsym
- dlopen dlsym dlclose dlerror
- 显示运行时链接的函数与例子,dlopen,dlsym,dlerror,dlclose
- Linux 中 dlopen、dlsym、dlclose、dlerror函数
- dlopen dlsym dlclose dlerror【zz】
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror【zt】
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror【zt】
- linux下动态链接库的使用-dlopen dlsym dlclose dlerror
- linux下动态链接库的使用-dlopen dlsym dlclose dlerror
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror【zt】
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
- django常见错误
- ORACLE中排序ORDER BY
- C++ fork()以及缓冲结合考点
- CNN的反向求导及练习
- Kali Linux 无线渗透测试入门指南 第三章 绕过 WLAN 身份验证
- linux中的显式运行时链接---dlopen、dlsym、dlerror、dlclose搞起
- 最长公共子序列
- 小白日记36:kali渗透测试之Web渗透-手动漏洞挖掘(二)-突破身份认证,操作系统任意命令执行漏洞
- python 实现ftp文件下载
- Ubuntu日记——安装和配置Lavarel项目
- tensorflow学习笔记(六):LSTM 与 GRU
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) 部分题解
- Eclipse工具使用技巧总结
- bindService(绑定服务) 流程源码解析