Linux+C+动态库(*.so文件)
在c中调用so文件有两种方法:
- 1.在编译时加入*.so文件,如:
gcc -o test test.c ./libtest.so
- 2.在代码中使用dlopen函数,该方法让我想起java中的反射,如:
dlopen("/lib/x86_64-linux-gnu/libm.so.6",RTLD_LAZY);
生成so文件
首先创建头文件test.h
#ifndef _TEST_H
#define _TEST_H
int add(int a, int b);
#endif
创建test.c
#include "test.h"
int add(int a, int b)
{
return a + b;
}
此时即可调用命令生成动态库(*.so文件)
gcc -shared -fPIC -o libtest.so test.c
使用第一种方法加载动态库
方法需要动态库的头文件
创建so_test.c
#include "test.h"
#include <stdio.h>
int main()
{
printf("%d\n",add(2,3));
return 1;
}
编译
gcc -o so_test so_test.c ./libtest.so
编译完成后,即可使用./so_test运行,可尝试删除libtest.so文件,再次运行,查看运行结果
使用第二种方法加载动态库
创建foo.c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
void *handle;
char *error;
//创建函数指针,用于指向动态库中的函数
double (*cosine)(double);
//打开动态库并获取句柄
handle = dlopen("/lib/x86_64-linux-gnu/libm.so.6",RTLD_LAZY);
if(!handle)
{
//如果句柄为空,则输出错误信息
fputs(dlerror(),stderr);
exit(1);
}
//获取动态库中cos函数
cosine = dlsym(handle,"cos");
if((error = dlerror())!=NULL)
{
//如果有错误信息,则输出
fputs(error,stderr);
exit(1);
}
//调用cos函数
printf("%f\n",(*cosine)(30.0));
//关闭动态库
dlclose(handle);
return 0;
}
编译
gcc -o foo foo.c -ldl
-ldl 指示连接器连接一个库。这个库里包含了 dlopen, dlsym 等等的函数。也就是说,是支持“在运行时,显示加载使用动态连接库”的函数库。相关的头文件是 dlfcn.h
编译完成后,即可使用./foo运行,