参考大佬:
http://blog.sina.com.cn/s/blog_a4f2bd7d010114ka.html
test.h
#ifndef _TEST_H
#define _TEST_H
void test_a();
void test_b();
#endif
test_a.c
#include
#include "test.h"
void test_a()
{
printf("test in test_a..../n");
}
test_b.c
#include
#include "test.h"
void test_b()
{
printf("test in test_b..../n");
}
test.c
#include
#include "test.h"
int main()
{
test_a();
test_b();
return 0;
}
main.c
#include
#include
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
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就再也不能使用它的符号了。