当前位置:网站首页>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
边栏推荐
- 使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
- Merging of Shanzhai version [i]
- Neodynamic Barcode Professional for WPF V11.0
- C language --- advanced pointer
- Interview questions of a blue team of Beijing Information Protection Network
- Mumu, go all the way
- 控制结构(一)
- The principle and common methods of multithreading and the difference between thread and runnable
- fatal error: torch/extension. h: No such file or directory
- Upgrade MySQL 5.1 to 5.67
猜你喜欢
随机推荐
字符串排序
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
Treatment of idempotency
Go language slice, range, set
gps北斗高精度卫星时间同步系统应用案例
[backtrader source code analysis 18] Yahoo Py code comments and analysis (boring, interested in the code, you can refer to)
现在做自媒体能赚钱吗?看完这篇文章你就明白了
控制结构(一)
Go并发和通道
fatal error: torch/extension. h: No such file or directory
Codejock Suite Pro v20.3.0
Why disable foreign key constraints
pgpool-II 4.3 中文手册 - 入门教程
实现缺省页面
计算某字符出现次数
Upgrade MySQL 5.1 to 5.68
The El tree implementation only displays a certain level of check boxes and selects radio
为啥禁用外键约束
大厂技术实现 | 行业解决方案系列教程
编译,连接 -- 笔记









