JNI加载hal的dlopen()相关操作
1.函数集合
#include <dlfcn.h> void *dlopen(const char *filename, int flag); char *dlerror(void); void *dlsym(void *handle, const char *symbol); int dlclose(void *handle);
Link with -ldl.
2.Demo例子
caculate.c用于编译成一个库

int add(int a,int b) { return (a + b); } int sub(int a, int b) { return (a - b); } int mul(int a, int b) { return (a * b); } int div(int a, int b) { return (a / b); }
main.c调用这个库里面的函数

#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> #define LIB_CACULATE_PATH "./libcaculate.so" typedef int (*CAC_FUNC)(int, int); int main() { void *handle; char *error; CAC_FUNC cac_func = NULL; handle = dlopen(LIB_CACULATE_PATH, RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); return -1; } //获取一个函数 cac_func = dlsym(handle, "add"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", error); return -1; } printf("add: 2+7=%d\n", cac_func(2,7)); cac_func = dlsym(handle, "sub"); printf("sub: 9-2=%d\n", cac_func(9,2)); cac_func = dlsym(handle, "mul"); printf("mul: 3*2=%d\n", cac_func(3,2)); cac_func = dlsym(handle, "div"); printf("div: 8/2=%d\n", cac_func(8,2)); dlclose(handle); return 0; }
编译执行:

编译: $ gcc -rdynamic -o main main.c -ldl $ gcc -shared -fPIC caculate.c -o libcaculate.so 执行: $ ./main add: 2+7=9 sub: 9-2=7 mul: 3*2=6 div: 8/2=4
或者使用一个结构体将所有的函数包装起来,这样只需要调用一次dlsym()

struct math { int (*add)(int a,int b); int (*sub)(int a,int b); int (*mul)(int a,int b); int (*div)(int a,int b); }; static int math_add(int a,int b) { return (a + b); } static int math_sub(int a, int b) { return (a - b); } static int math_mul(int a, int b) { return (a * b); } static int math_div(int a, int b) { return (a / b); } /*shouldn't be static, else dlsym() couldn't find it*/ struct math HMI = { .add = math_add, .sub = math_sub, .mul = math_mul, .div = math_div, };

#include <stdio.h> #include <stdlib.h> #include <dlfcn.h> struct math { int (*add)(int a,int b); int (*sub)(int a,int b); int (*mul)(int a,int b); int (*div)(int a,int b); }; #define LIB_CACULATE_PATH "./libcaculate.so" typedef int (*CAC_FUNC)(int, int); int main() { void *handle; char *error; struct math *hmi; handle = dlopen(LIB_CACULATE_PATH, RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); return -1; } //获取一个函数 hmi = dlsym(handle, "HMI"); if ((error = dlerror()) != NULL) { fprintf(stderr, "%s\n", dlerror()); return -1; } printf("add: 2+7=%d\n", hmi->add(2,7)); printf("sub: 9-2=%d\n", hmi->sub(9,2)); printf("mul: 3*2=%d\n", hmi->mul(3,2)); printf("div: 8/2=%d\n", hmi->div(8,2)); dlclose(handle); return 0; }
3.详细信息:# man dlopen
posted on 2019-04-29 14:33 Hello-World3 阅读(630) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!