当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
mysql乐观锁解决并发冲突
怎么看基金是不是reits,通过银行购买基金安全吗
Implement default page
Application of Bloom filter in 100 million flow e-commerce system
Distinct use of spark operator
New developments: new trends in cooperation between smartmesh and meshbox
Redis master-slave replication process
Go语言条件,循环,函数
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
Go language, condition, loop, function
dlopen/dlsym/dlclose的简单用法
Neodynamic Barcode Professional for WPF V11.0
计算某字符出现次数
Accumulation of applet knowledge points
【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
Vision of building interstellar computing network
运维流程有多重要,听说一年能省下200万?
MySQL optimistic lock to solve concurrency conflict
王启亨谈Web3.0与价值互联网“通证交换”
Treatment of idempotency