当前位置:网站首页>dlopen/dlsym/dlclose的简单用法
dlopen/dlsym/dlclose的简单用法
2022-04-23 15:47:00 【xlbtlmy】
main.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#define DLL_PATH_ADD "./libadd.so"
#define DLL_PATH_SUB "./libsub.so"
typedef int (*func)(int, int);
int main()
{
{
void *dlhandler_add;
func func_add = NULL;
dlhandler_add = dlopen(DLL_PATH_ADD, RTLD_LAZY);
if (NULL == dlhandler_add)
{
fprintf(stderr, "%s\n", dlerror());
exit(-1);
}
dlerror();
func_add = dlsym(dlhandler_add, "add");
if (func_add)
printf("%d\n", func_add(1, 2));
dlclose(dlhandler_add);
}
{
void *dlhandler_sub;
func func_sub = NULL;
dlhandler_sub = dlopen(DLL_PATH_SUB, RTLD_LAZY);
if (NULL == dlhandler_sub)
{
fprintf(stderr, "%s\n", dlerror());
exit(-1);
}
dlerror();
func_sub = dlsym(dlhandler_sub, "sub");
if (func_sub)
printf("%d\n", func_sub(1, 2));
dlclose(dlhandler_sub);
}
return 0;
}
add.c
int add(int a, int b)
{
return (a + b);
}
sub.c
int sub(int a, int b)
{
return (a - b);
}
Makefile
all:
gcc -g add.c -shared -fPIC -o libadd.so
gcc -g sub.c -shared -fPIC -o libsub.so
gcc -g main.c -rdynamic -ldl -o test
./test
clean:
rm -rf test *.so
运行结果如下
3
-1
版权声明
本文为[xlbtlmy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u011958166/article/details/124335547
边栏推荐
- Extract non duplicate integers
- 单体架构系统重新架构
- Multi level cache usage
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- Spark 算子之partitionBy
- 一刷314-剑指 Offer 09. 用两个栈实现队列(e)
- Go并发和通道
- s16. One click installation of containerd script based on image warehouse
- Fastjon2他来了,性能显著提升,还能再战十年
- CVPR 2022 优质论文分享
猜你喜欢
JVM-第2章-类加载子系统(Class Loader Subsystem)
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
新动态:SmartMesh和MeshBox的合作新动向
Spark 算子之交集、并集、差集
c语言---指针进阶
Sorting and replying to questions related to transformer
What if the server is poisoned? How does the server prevent virus intrusion?
Cookie&Session
单体架构系统重新架构
随机推荐
计算某字符出现次数
Spark 算子之coalesce与repartition
Timing model: gated cyclic unit network (Gru)
多生成树MSTP的配置
字符串排序
JVM-第2章-类加载子系统(Class Loader Subsystem)
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
删除字符串中出现次数最少的字符
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
MySQL Cluster Mode and application scenario
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Independent operation smart farm Innovation Forum
网站压测工具Apache-ab,webbench,Apache-Jemeter
Codejock Suite Pro v20. three
leetcode-396 旋转函数
Treatment of idempotency
Mobile finance (for personal use)
Go concurrency and channel
Deletes the least frequently occurring character in the string