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. 用几句话说一说CMake add_dependencies & target_link_libraries的使用区别

    简单说一说前两天学习使用CMake解决链接问题时遇到的一个问题. 对于编译时遇到的依赖问题,很多时候我们只需要一句target_link_libraries就可以搞定. 但是CMake还有另外一个co ...

  2. Windows创建Sciter的第一个程序.HelloWorld

    介绍什么的就免了.直接进入正题 平台: Windows 10 IDE : Visual studio 2017 首先从官网下载最新的SDK,https://sciter.com/download/ 创 ...

  3. 常用的字符串函数-S

    header('content-type:text/html;charset=utf-f'); /* $var=addslashes($_GET['username']);//转义表单提交内容中的引号 ...

  4. “ORA-12154: TNS: 无法解析指定的连接标识符”错误解决办法

    某政府项目中用.Net网站连接Oracle数据库,web.config使用的连接字符串如下: <add key="connstring_ora" value="Us ...

  5. GMT5 install

    there are two imporant modules called gshhg and dcw when installing GMT5; try to state the locations ...

  6. MySQL数据库语句

    一 . 常用mysql命令行命令        1 .启动MYSQL服务   net start mysql 停止MYSQL服务   net stop mysql 2 . netstat –na | ...

  7. sed命令替换目录

    1.把目录/home/dwi_pro/csliyb1替换成/home/dwi_pro/csliyb2 sed -i "s:/home/dwi_pro/csliyb1:/home/dwi_pr ...

  8. Oracle学习DayFour(高级子查询)

    一.高级子查询 1.多列子查询 定义:主查询与子查询返回的多个列进行比较 多列子查询中的比较分为两种:成对比较:不成对比较 实例:查询与141号或174号员工的manager_id和departmen ...

  9. Displaylink安卓驱动

    Displaylink安卓驱动 2019年最新版V2.3.1

  10. stylus导入时 报错These relative modules were not found

    These relative modules were not found: * ./star48_@2x.png in ./node_modules/_css-loader@0.28.7@css-l ...