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);
}
View Code
复制代码

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;
}
View Code
复制代码

编译执行:

复制代码
编译:
$ 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
View Code
复制代码

 

或者使用一个结构体将所有的函数包装起来,这样只需要调用一次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,
};
View Code
复制代码
复制代码
#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;
}
View Code
复制代码

 

3.详细信息:# man dlopen

 

posted on   Hello-World3  阅读(630)  评论(0编辑  收藏  举报

编辑推荐:
· 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框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示