当前位置:网站首页>Simple usage of dlopen / dlsym / dlclose
Simple usage of dlopen / dlsym / dlclose
2022-04-23 15:52: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
The operation results are as follows
3
-1
版权声明
本文为[xlbtlmy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231547255895.html
边栏推荐
- JS regular determines whether the port path of the domain name or IP is correct
- Go language, condition, loop, function
- Upgrade MySQL 5.1 to 5.67
- How do you think the fund is REITs? Is it safe to buy the fund through the bank
- Why is IP direct connection prohibited in large-scale Internet
- Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
- Sortby use of spark operator
- 一文掌握vscode远程gdb调试
- For examination
- Config组件学习笔记
猜你喜欢
为啥禁用外键约束
Codejock Suite Pro v20. three
IronPDF for . NET 2022.4.5455
Sortby use of spark operator
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
Redis master-slave replication process
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
Codejock Suite Pro v20.3.0
Unity Shader学习
多线程原理和常用方法以及Thread和Runnable的区别
随机推荐
shell脚本中的DATE日期计算
Go语言数组,指针,结构体
gps北斗高精度卫星时间同步系统应用案例
Coalesce and repartition of spark operators
Upgrade MySQL 5.1 to 5.67
多级缓存使用
MySQL集群模式與應用場景
C language --- advanced pointer
布隆过滤器在亿级流量电商系统的应用
Go language, condition, loop, function
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
Treatment of idempotency
CAP定理
String sorting
Unity Shader学习
What if the server is poisoned? How does the server prevent virus intrusion?
一刷313-剑指 Offer 06. 从尾到头打印链表(e)
Large factory technology implementation | industry solution series tutorials
Why is IP direct connection prohibited in large-scale Internet
为啥禁用外键约束