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 -;
} //获取一个函数
cac_func = dlsym(handle, "add");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
return -;
}
printf("add: 2+7=%d\n", cac_func(,)); cac_func = dlsym(handle, "sub");
printf("sub: 9-2=%d\n", cac_func(,)); cac_func = dlsym(handle, "mul");
printf("mul: 3*2=%d\n", cac_func(,)); cac_func = dlsym(handle, "div");
printf("div: 8/2=%d\n", cac_func(,)); dlclose(handle); return ;
}

编译执行:

编译:
$ gcc -rdynamic -o main main.c -ldl
$ gcc -shared -fPIC caculate.c -o libcaculate.so
执行:
$ ./main
add: +=
sub: -=
mul: *=
div: /=

或者使用一个结构体将所有的函数包装起来,这样只需要调用一次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 -;
} //获取一个函数
hmi = dlsym(handle, "HMI");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", dlerror());
return -;
}
printf("add: 2+7=%d\n", hmi->add(,));
printf("sub: 9-2=%d\n", hmi->sub(,));
printf("mul: 3*2=%d\n", hmi->mul(,));
printf("div: 8/2=%d\n", hmi->div(,)); dlclose(handle); return ;
}

3.详细信息:# man dlopen

JNI加载hal的dlopen()相关操作的更多相关文章

  1. NDK jni 加载静态库

    加载静态库到android,静态库的提供方式有2种, a. 通过源文件来编译静态库 b. 加载已经编译好的静态库 首先我们来看,通过源文件来编译静态库,工程目录如下 第一步:我们来看我们的jni目录, ...

  2. JNI加载Native Library 以及 跨线程和Qt通信

    Part1 Java Native Interface-JNI-JAVA本地调用 JNI标准是Java平台的一部分, 允许Java代码和其他语言进行交互; 开始实现-> Step 1) 编写Ja ...

  3. android java层通过jni加载使用第三方的so库

    1.例如我们自己编译一个so库,我们的其他模块要加载如何操作了 首先在c盘新建立一个文件夹sb,在sb下面新建立一个文件夹jni,如果你要使用ndk编译so库,必须需要有jni目录 2.在jni目录下 ...

  4. 在前端页面对easyui中的datagrid与jqgrid加载后的数据进行操作

    因为项目的需求,需要在grid中加载数据后再在前端页面执行操作,所以在easyui中的grid与jqgrid都进行了测试和操作: eayui中grid数据的操作: //构造集合对象 var list ...

  5. 前端笔记之jQuery(上)加载函数的区别&对象&操作HTML/CSS&动画&选择器

    一.jQuery简介 1.0 JavaScript编程比较恶心的地方 恶心1:选择元素麻烦,全线兼容的方法只有getElementById()和getElementsByTagName()两个.其他的 ...

  6. 判断iframe加载完成、用于当ifame加载完成时执行一些操作

    window.frames["iframec"].addEventListener( "load", function(){ window.frames[&qu ...

  7. spring boot容器加载完后执行特定操作

    有时候我们需要在spring boot容器启动并加载完后,开一些线程或者一些程序来干某些事情.这时候我们需要配置ContextRefreshedEvent事件来实现我们要做的事情 1.Applicat ...

  8. so加载报错:dlopen failed: couldn't map ... Permission denied

    转自:https://blog.csdn.net/u013270444/article/details/60869376 问题描述: 我的应用当中集成了一个安全相关的sdk,而这个sdk中使用的so是 ...

  9. as3 对于加载进来多层swf缩放操作

    //swf实际尺寸 var oldWidth:Number = frameLder.contentLoaderInfo.content.width; var oldHeight:Number = fr ...

随机推荐

  1. AJAX的简单示例:注册校验

    众所周知,我们每次需要注册一个网站的用户名时,都会校验该邮箱.用户名是不是正确的格式.是不是有被使用过,密码是否符合规则,二次确认是否符合. 如果这些校验都采用form表单提交的话,会给用户带来极不好 ...

  2. 责任链模式(Chain of Responsibility)

    定义:为一个请求定义含有链状关系的接受对象,基于请求的类型,松耦合发送者和接受者之间的关系. 实现方式: 首先定义一个抽象类,包括一个公共抽象行为和决定子类链状关系的属性,然后创建一系列对象继承这个抽 ...

  3. TCP端口检测工具

    很多时候,我们需要测试 tcp 端口.ping 命令虽然好用,但不能测试端口,因为 ping 基于ICMP协议,属于IP层协议,所以无法测试传输层的 TCP/UDP 端口.幸好有tcping命令,可以 ...

  4. IP通信基础原理课堂笔记----HCL(1)

    PC端配置:打开接口,配置ip地址 交换机配置:①创建VLAN system-view vlan 10 vlan 20 ②配置PC端接口 interface gi 1/0/1 port link-ty ...

  5. JavaScript几种常见的继承方法

    1.call() 方法 call() 方法是与经典的对象冒充方法最相似的方法.它的第一个参数用作 this 的对象.其他参数都直接传递给函数自身 function Huster(name,idNum, ...

  6. Hadoop学习------Hadoop安装方式之(三):分布式部署

    这里为了方便直接将单机部署过的虚拟机直接克隆,当然也可以不这样做,一个个手工部署. 创建完整克隆——>下一步——>安装位置.等待一段时间即可. 我这边用了三台虚拟机,分别起名master, ...

  7. QRCode.js生成二维码

    QRCode的GitHub地址: https://github.com/KeeeX/qrcodejs 该版本解决了主版本(https://github.com/davidshimjs/qrcodejs ...

  8. Python Django install Error

    Exception:Traceback (most recent call last):  File "/home/djangogirls/myvenv/lib/python3.6/site ...

  9. Delphi7第三方控件

    控件安装(安装时建议先关闭Delphi) 1.只有一个DCU文件的组件. DCU文件是编译好的单元文件,这样的组件是作者不想把源码公布.一般来说,作者必须说明此组件适合Delphi的哪种版本,如果版本 ...

  10. 关于toLocaleDateString的坑

    https://segmentfault.com/a/1190000009391790