当前位置:网站首页>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
边栏推荐
- Upgrade MySQL 5.1 to 5.611
- Independent operation smart farm Innovation Forum
- 大厂技术实现 | 行业解决方案系列教程
- JVM - Chapter 2 - class loader subsystem
- 建设星际计算网络的愿景
- leetcode-374 猜数字大小
- 贫困的无网地区怎么有钱建设网络?
- Deletes the least frequently occurring character in the string
- Why is IP direct connection prohibited in large-scale Internet
- Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
猜你喜欢

多生成树MSTP的配置

Spark 算子之交集、并集、差集

Do we media make money now? After reading this article, you will understand

Neodynamic Barcode Professional for WPF V11. 0

Temporal model: long-term and short-term memory network (LSTM)

API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)

多级缓存使用

Redis master-slave replication process

APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)

cadence SPB17. 4 - Active Class and Subclass
随机推荐
C language --- advanced pointer
JVM - Chapter 2 - class loader subsystem
c语言---字符串+内存函数
gps北斗高精度卫星时间同步系统应用案例
Basic greedy summary
Spark 算子之filter使用
移动金融(自用)
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
MySQL Cluster Mode and application scenario
One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
Deletes the least frequently occurring character in the string
时序模型:长短期记忆网络(LSTM)
考试考试自用
Calculate the number of occurrences of a character
布隆过滤器在亿级流量电商系统的应用
Go并发和通道
s16.基于镜像仓库一键安装containerd脚本
[split of recursive number] n points K, split of limited range
Upgrade MySQL 5.1 to 5.67
Special analysis of China's digital technology in 2022