C语言 dlopen dlsym
C语言加载动态库
头文件:#include<dlfcn.h>
void * dlopen(const char* pathName, int mode); 返回值 handle
void *dlsym(void *handle, const char* symbol); 返回值 函数起始地址
handle是使用dlopen函数之后返回的句柄,symbol是要求获取的函数的名称,函数,返回值是void*,指向函数的地址;
测试:
创建一个.c文件,编译成动态链接库
/*************************************************************************
> File Name: hello.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时39分42秒
************************************************************************/ #include<stdio.h> int hello() {
printf("hello c \n");
return ;
}
gcc -shared -fPIC hello.c -o hello.so
创建一个头文件申明hello函数
/*************************************************************************
> File Name: hello.h
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时40分25秒
************************************************************************/ #ifndef _HELLO_H
#define _HELLO_H int hello(); #endif
创建一个main.c用于测试编译的hello.so是否正确
/*************************************************************************
> File Name: mian.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时41分22秒
************************************************************************/ #include<stdio.h>
#include"hello.h" int main() {
hello();
return ;
}
使用动态链接库编译
gcc main.c -o app ./hello.so
运行编译好的app文件输出如下
hello c
证明动态链接库编译成功,接下来使用dlopen和dlsym调用hello.so内的hello函数
/*************************************************************************
> File Name: mian.c
> Author:
> Mail:
> Created Time: 2019年12月12日 星期四 14时41分22秒
************************************************************************/ #include<stdio.h>
#include<dlfcn.h>
#include"hello.h" typedef int (*callback)();
static callback cb; int main() {
void* handle;
handle = dlopen("./hello.so",RTLD_LAZY);
if (!handle) {
printf("handle is null \n");
return ;
}
cb = (callback)dlsym(handle, "hello");
cb();
return ;
}
编译main.c 需要链接静态库
gcc main.c -o app -ldl
运行程序
hello c
可以发现与之前使用动态链接库编译运行结果相同。
C语言 dlopen dlsym的更多相关文章
- dlopen, dlsym今天才刚知道干什么用的,羞死人了
dlopen, dlsym今天才刚知道干什么用的,羞死人了
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror(转)
dlopen 基本定义 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * pathn ...
- LINUX下动态链接库的使用-dlopen dlsym dlclose dlerror
本定义 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h> 函数定义: void * dlopen( const char * pathname, int mod ...
- 加载动态链接库——dlopen dlsym dlclose
DLOPEN DLMOPEN DLCLOSE NAME dlclose, dlopen, dlmopen - 打开/关闭共享对象 SYNOPSIS #include <dlfcn.h&g ...
- C - dlopen dlsym
-----------------------------------------------------------------------------dlsym------------------ ...
- C语言动态调用库(转)
转自:http://cloverprince.iteye.com/blog/481309 现有一个主程序用C语言写成.现在要允许第三方开发人员编写扩展的模块,约定第三方开发的模块必须提供一系列已知名称 ...
- C 语言 和 python 调用 .so 文件
什么是静态库和动态库, 看一篇博客 http://www.cnblogs.com/skynet/p/3372855.html 现在,我们首先生成.so文件 首先, 我们写一个a.c文件 1 2 3 4 ...
- 执行dlsym()函数出现: undefined symbol
执行dlsym()函数出现: undefined symbol 执行dlsym()函数出现: undefined symbol 当这个问题出现的时候,可以检查产生so文件的cpp文件,看看是否已经用 ...
- 精通UNIX下C语言编程与项目实践
cc -I //include 目录 -L //静态库目录?动态也可以 -l //小写L,接静态库名称?动态也可以 -DXXX='"XXFF"' //-D直接定义宏 -c 只编 ...
随机推荐
- 一篇文章看懂mysql中varchar能存多少汉字、数字,以及varchar(100)和varchar(10)的区别
看完这篇文章,你能搞清楚以下问题: 1.varchar(100)和varchar(10)的区别在哪里? 2.varchar能存多少汉字.数字? 3.varchar的最大长度是多少呢? 4.字符.字节. ...
- HttpClient代理IP及设置连接读取超时
1.不废话,上代码: public static void main(String[] args) throws Exception { CloseableHttpClient httpClient ...
- 解决solr 请求参数过长报错too many boolean clauses Exception
booleanClauses属性的意义 貌似是查询条件有几个逻辑判断而不是参数长度. 如下面两种情况 a:1 OR b:2 AND C:3那么此时booleanClauses=3 id(1 2 3 4 ...
- linux的arp表满导致同网段无法ping通
由于历史原因,有一个网段子网设置非常大10.0.0.0/21,8个C地址段为一个子网. linux内核默认arp表大小为1024,导致一台监控机器arp表溢出,同时导致日志输出速率超出限制,无法输出日 ...
- stacking method house price in kaggle top10%
整合几部分代码的汇总 隐藏代码片段 导入python数据和可视化包 导入统计相关的工具 导入回归相关的算法 导入数据预处理相关的方法 导入模型调参相关的包 读取数据 特征工程 缺失值 类别特征处理-l ...
- linux 操作文件夹
创建文件夹[mkdir] 一.mkdir命令使用权限 所有用户都可以在终端使用 mkdir 命令在拥有权限的文件夹创建文件夹或目录. 二.mkdir命令使用格式 格式:mkdir [选项] DirNa ...
- git cherry-pick 命令,解决冲突后没有 commit 变更,再次pull 时出错
Git : You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists). 解决方法:(1)使用git status 命令查看当前 ...
- [转帖]Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案
Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案 https://www.cnblogs.com/xlecho/p/11834011.html echo编辑整理,欢迎转载,转 ...
- 【数据结构与算法】线性表操作(C语言)
#include <stdio.h> #include <stdlib.h> #define OK 1 #define NO 0 #define MAXSIZE 20 type ...
- 在vue中导出excel表格
初学者学习vue开发,想把前端项目中导出Excel表格,查了众多帖子,踩了很多坑,拿出来与大家分享一下经验. 安装依赖 //npm npm install file-saver -S npm inst ...