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. scrapy_redis项目配置

    一.创建普通scrapy项目 二.spiders爬虫文件中修改项 import scrapy from XX.items import XXItem import json # ----1 导入类 f ...

  2. 第十节 集合类Collection和Map

    接口 Collection<E>  (E)代表类型 集合类: 与数组的异同: 数组:数组虽然也可以存储对象,但长度是固定的:集合的长度是可变的,数组中可以存储基本数据类型,集合只能存储对象 ...

  3. LR单用户,重复操作日志

    案例:假如你想在一个脚本中,实现登录执行1次,查询执行2次,插入执行3次,怎么办?录3个脚本?每个事务分别在脚本中复制N次? 当然不用,LR早就想到了你的需求,下面让我们隆重推出Block. 位置: ...

  4. ionic3 应用内打开第三方地图导航 百度 高德

    1.安装检测第三方APP是否存在的插件 cordova plugin add cordova-plugin-appavailability --save npm install --save @ion ...

  5. windows中obs源码编译的坑

    好用的版本: cmake-3.6.1-win64-x64  +  vs2015  + qt-opensource-windows-x86-msvc2015_64-5.7.0   +   obs-stu ...

  6. Liunx find/locate/whereis/which 总结

    一.locate 命令 是一个文件查找命令,命令所属软件包 mlocate 不同于 find 命令的是,find命令在整块磁盘中搜索:而 locate命令 在数据路库文件中搜索,当天创建的文件第二天才 ...

  7. [ 转 ] RESTful

    一.什么是RESTful 定义: REST全程是Representational State Transfer,表述性状态转移.它首次出现在2000年Roy Fielding的博士论文中,Roy Fi ...

  8. mssql server 数据库帮助类

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Web ...

  9. 第二次scrum冲刺

    一.第二次冲刺任务         Scrum是在已有的基础上实现读者查询书籍的功能. 二.用户故事         用户输入账号.密码   用户输入需要查询的书籍   系统显示用户输入的信息的详细信 ...

  10. python初学之缓存清理:完全相同的代码与环境但是其中一个文件可以执行成功,一个执行不成功

    在使用python写接口测试脚本时,想要引入logging模块来在控制台输出当前执行进度日志,但是遇到了奇葩问题,困扰了一整个下午: 代码如下: __author__ = 'test'#!/usr/b ...