当前位置:网站首页>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
边栏推荐
- PHP classes and objects
- R语言中绘制ROC曲线方法二:pROC包
- MySQL Cluster Mode and application scenario
- Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
- VIM specifies the line comment and reconciliation comment
- Unity Shader学习
- For examination
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- 【第5节 if和for】
- One brush 314 sword finger offer 09 Implement queue (E) with two stacks
猜你喜欢

Pgpool II 4.3 Chinese Manual - introductory tutorial

Intersection, union and difference sets of spark operators

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

布隆过滤器在亿级流量电商系统的应用

cadence SPB17. 4 - Active Class and Subclass

大型互联网为什么禁止ip直连

Coalesce and repartition of spark operators

C语言自编字符串处理函数——字符串分割、字符串填充等

Load Balancer

Neodynamic Barcode Professional for WPF V11. 0
随机推荐
Date date calculation in shell script
s16. One click installation of containerd script based on image warehouse
Do we media make money now? After reading this article, you will understand
Groupby use of spark operator
One brush 313 sword finger offer 06 Print linked list from end to end (E)
APISIX jwt-auth 插件存在错误响应中泄露信息的风险公告(CVE-2022-29266)
shell脚本中的DATE日期计算
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
MySQL集群模式与应用场景
What if the server is poisoned? How does the server prevent virus intrusion?
R语言中实现作图对象排列的函数总结
负载均衡器
API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
Cookie&Session
Spark 算子之sortBy使用
Sortby use of spark operator
字符串排序
leetcode-396 旋转函数
dlopen/dlsym/dlclose的简单用法
布隆过滤器在亿级流量电商系统的应用