动态库的生成与对.so的读取

来源:互联网 发布:java报表怎么做 编辑:程序博客网 时间:2024/06/02 03:16

好久没弄动态库了

编个小程序来热手一下

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

 

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就再也不能使用它的符号了。

 

 

原创粉丝点击