当前位置:网站首页>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
边栏推荐
- Fastjon2他来了,性能显著提升,还能再战十年
- Advantages, disadvantages and selection of activation function
- c语言---字符串+内存函数
- s16. One click installation of containerd script based on image warehouse
- Today's sleep quality record 76 points
- Best practices of Apache APIs IX high availability configuration center based on tidb
- Go语言切片,范围,集合
- 山寨版归并【上】
- 使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
猜你喜欢
Import address table analysis (calculated according to the library file name: number of imported functions, function serial number and function name)
Multitimer V2 reconstruction version | an infinitely scalable software timer
基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
Spark 算子之distinct使用
Single architecture system re architecture
Cap theorem
Neodynamic Barcode Professional for WPF V11.0
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
pgpool-II 4.3 中文手册 - 入门教程
移动金融(自用)
随机推荐
Application case of GPS Beidou high precision satellite time synchronization system
c语言---指针进阶
建设星际计算网络的愿景
Neodynamic Barcode Professional for WPF V11. 0
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
Code live collection ▏ software test report template Fan Wen is here
Spark 算子之coalesce与repartition
leetcode-374 猜数字大小
[self entertainment] construction notes week 2
网站压测工具Apache-ab,webbench,Apache-Jemeter
Upgrade MySQL 5.1 to 5.67
Do we media make money now? After reading this article, you will understand
Modèle de Cluster MySQL et scénario d'application
Redis master-slave replication process
What if the server is poisoned? How does the server prevent virus intrusion?
怎么看基金是不是reits,通过银行购买基金安全吗
Mobile finance (for personal use)
c语言---字符串+内存函数
Best practices of Apache APIs IX high availability configuration center based on tidb
多生成树MSTP的配置