一、 使用libdl.so库


动态库加载原理


动态库中函数的查找已经封装成哭libdl.so


libdl.so里面有4个函数:


dlopen//打开一个动态库


dlsym//在打开的动态库里找一个函数


dlclose//关闭动态库


dlerror//返回错误




dl.c


#include <stdio.h>
#include <dlfcn.h>

int main(){
   void* handler = dlopen("./libdemo1.so",RTLD_LAZY);
   int (*fun)(int,int) = dlsym(handler,"add");
   int result = fun(34,25);

   printf("the result of teo number is %d\n",result);

}




在命令行中输入的命令:


[fedora@localhost day02_r3]$ gcc dl.c -odl -ldl
[fedora@localhost day02_r3]$ ./ld



----------------------------------libdl.so的基本使用的小结----------------------------------------


只需要记住一个头文件,和四个函数即可:


头文件:#include <dlfcn.h>



四个函数:


dlopen//打开一个动态库



dlsym//在打开的动态库里找一个函数



dlclose//关闭动态库



dlerror//返回错误





头文件和上面的四个函数的用法都不需要死记,旺季的时候man一下即可。。。。。




 总结:



  1. 编译连接动态库



  2. 使用动态库



  3. 怎么配置让程序调用动态库



  4. 掌握某些工具的使用 nm ldd lddconfig objdump strit(去掉多余的信息)