#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;
}