2020-12-23 C语言动态打开so库

#include <stdio.h>
#include <dlfcn.h>

//编译so库文件:gcc jaccard.c -I . -shared -fPIC -o libjaccard.so

int load_so(char* path, char* so)
{
    //加载指定位置的so动态库
	int ret = 100;
	int rate = 0;
    char so_path[128] = {'\0'};
    snprintf(so_path, sizeof(so_path), "%s%s", path, so);

    printf("open so(%s) is %s\n", so, so_path);

    void* so_handle = dlopen(so_path, RTLD_LAZY);
    if (NULL != so_handle) {
		printf("NOT NULL open so(%s) is %s\n", so, so_path);
		int (*test_calculate)(int ret); //test_calculate的函数原型需要和calculate保持一致
		//根据动态链接库操作句柄与符号,返回符号对应的地址
		test_calculate = dlsym(so_handle, "calculate"); //calculate 为libjaccard.so中的具体的函数名
		if (NULL == test_calculate) {
			netmon_fuzzy_match_debug("[ERROR]func is NULL\n");
		} else {
			rate = test_calculate(ret);
			printf("NULL open so(%s) is %s\n", so, so_path);
		}
        return rate;
    } else {
		printf("NULL open so(%s) is %s\n", so, so_path);
		return 0;
    }
}

int main()
{
	load_so("/usr/lib64/", "libjaccard.so");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值