linux 动态加载动态库
来源:互联网 发布:京东商城一号店淘宝 编辑:程序博客网 时间:2024/04/30 08:37
linux 动态加载动态库
动态加载库
在Linux中可以动态加载库,其使用方法如下:
1. 先生成一个动态库libtest.so
/* test.c */
#include <stdio.h>
#include <stdio.h>
void test1(int no)
{
printf("*****************************************\n");
printf("This is test1, the number is %d.\n", no);
printf("*****************************************\n");
}
void test2(char *str)
{
printf("=========================================\n");
printf("This is test2, the string is %s.\n", str);
printf("=========================================\n");
}
编译库:
gcc -fPIC -shared -o libtest.so test.c
这样就可以生成libtest.so动态库。
在这个库里,定义个两个函数test1,test2,下面将在程序中加载libtest.so,然后调用test1,test2。
2. 动态加载libtest.so
/* main.c */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dlfcn.h> /* 必须加这个头文件 */
#include <assert.h>
int main()
{
void *handler = dlopen("libtest.so", RTLD_NOW);
assert(handler != NULL);
void (*test1)(int) = dlsym(handler, "test1");
assert(test1 != NULL);
void (*test2)(char *) = dlsym(handler, "test2");
assert(test2 != NULL);
(*test1)(10);
(*test2)("hello");
dlclose(handler);
return 0;
}
/* end */
在这个程序中,dlopen函数用来打开一个动态库,其返回一个void *的指针,如果失败,返回NULL。
dlsym返回一个动态库中的一个函数指针,如果失败,返回NULL。
dlclose关闭指向动态库的指针。
编译的时候需要加上 -ldl
gcc -o main main.c -ldl
运行main,将会看到调用test1,和test2的结果。
- linux 动态加载动态库
- linux 动态加载动态库
- linux 动态加载动态库
- linux 动态加载 动态链接库
- linux如何动态加载动态库
- linux 动态加载 动态链接库
- Linux 动态库动态加载实例
- linux动态加载动态链接库
- Linux动态加载动态链接库
- Linux 动态加载动态库(.so)
- linux 动态链接库加载
- linux共享库动态加载
- linux 中的动态库加载
- Linux下动态加载库
- linux 动态链接库加载
- linux 中的动态库加载
- Linux动态加载库剖析
- linux中加载动态库
- Java Business Integration
- 英语练习123 The Eiffel Tower
- 用 Qt Creator 开发非 Qt 的 C/C++ 程序
- 深入浅出 Cocoa 之 Core Data(2)- 代码示例
- linux音频alsa-uda134x驱动文档阅读之四(数字音频接口)
- linux 动态加载动态库
- Launcher实现左右循环滑动
- SQL优化查询建议(个人总结 仅供参考)
- SQL优化查询建议(个人总结 仅供参考)
- Android平台各类恶意软件及病毒概览
- 数据库中的一些名词定义
- C语言中malloc()函数与alloc()函数
- 问答 ——将 iOS 应用的体积控制在 20MB 以内对于其下载量有很明显的影响吗?
- 利用Qt creator编写linux驱动