最近用到了dlopen函数,感觉挺有意思的,所以记录一下;
首先我们先编译一个动态库:
1、写一个add.c
#include<stdio.h>
#include "add.h"
int add(int a, intb)
{
return a+b;
}
其中add.h就是下面一句:
int add(int a, int b);
2、编译add.c:
[root@loadlibrary]# gcc -fPIC -c add.c
[root@loadlibrary]# gcc -shared -o test_add.so add.c
生成文件目录如下:
add.c
add.o
test_add.so
其中,test_add.so就是我们需要用来测试的动态库了;
接下来测试loadlibrary:
1、测试代码test.c:
#include<stdio.h>
#include<dlfcn.h>
int main()
{
void* dp=NULL;
int (*add_function)(int, int);
dp = dlopen("test_add.so",RTLD_LAZY );
if(dp==NULL)
{
pr