Linux+C+动态库(*.so文件)

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运行,

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值