linux 中的动态库加载
来源:互联网 发布:网络摄像机安装方案 编辑:程序博客网 时间:2024/06/11 17:00
在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");
}
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(编译时要使用共享库dl 其中有dlopen dlsynm dlerror dlclose 函数)
运行main,将会看到调用test1,和test2的结果
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(编译时要使用共享库dl 其中有dlopen dlsynm dlerror dlclose 函数)
运行main,将会看到调用test1,和test2的结果
http://www.cppblog.com/fwxjj/archive/2009/06/02/86600.html
- linux 中的动态库加载
- linux 中的动态库加载
- linux 动态加载动态库
- linux 动态加载动态库
- linux 动态加载动态库
- android中的动态库加载
- linux 动态链接库加载
- linux共享库动态加载
- Linux下动态加载库
- linux 动态链接库加载
- Linux动态加载库剖析
- linux中加载动态库
- linux下加载动态库
- Linux动态库加载失败
- linux 动态加载 动态链接库
- linux如何动态加载动态库
- linux 动态加载 动态链接库
- Linux 动态库动态加载实例
- 实例讲解MySQL联合查询
- Android下的任务和Activity栈
- mem.h函数库
- 修改CentOS5.5系统默认语言
- 关于socket阻塞与非阻塞情况下的recv、send、read、write返回值
- linux 中的动态库加载
- 打日志
- kerberos 认证为什么不适用local(本地域)
- 第五章 – Magento资源配置2
- 记录~
- copy string array to string list
- opencore和stagefright对比
- C#验证网络状态
- 练习题1~5