linux 动态链接库的创建和使用--动态连接
来源:互联网 发布:iphone照片备份软件 编辑:程序博客网 时间:2024/04/27 08:24
linux 动态链接库的创建和使用--动态连接
分类: C 编程 2012-03-25 17:01 568人阅读 评论(0)收藏 举报
linuxreferencefunctiondatec
/** author: hjjdebug
* date: 2012
* title: linux 动态链接库的创建和使用--动态连接
*/
动态连接,就是由调用者显式调用指定库,并获取对应库的函数入口地址
linux 动态链接库的创建和使用
1. 先创建一个动态链接库。源码如下:
$ cat max.cpp
extern "C"
{
int max(int a, int b)
{
return a>b? a:b;
}
}
加上extern "C", 是为了导出函数名称不用C++格式,而用C格式
编译生成动态库
g++ -shared -o libmax.so max.cpp
把库copy 到系统目录。
sudo cp libmax.so /lib
2. 再创建一个测试用例,源码如下:
gitserver@gitserver-desktop:~/share/android4.0.3/hjj/pc$ cat test_d.cpp
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h> // 动态加载的函数头文件
// int max(int a, int b);
typedef int (*maxptr)(int a, int b);
int main(int argc, char *argv[])
{
maxptr max; // 定义max 型函数指针
void *handle = dlopen("libmax.so",RTLD_LAZY);
if(!handle)
{
printf("error open librayry libmax.so");
exit(1);
}
max = (maxptr)dlsym(handle,"max");
if(!max)
{
char *err=dlerror();
printf("%s",err);
exit(2);
}
int a=max(3,5);
printf("the bigger is %d\n",a);
dlclose(handle);
return 0;
}
编译生成可执行文件
g++ -o test test.cpp -ldl
libdl.so 是必需的动态库
3. 运行可执行文件。
$ ./test
the bigger is 5
注意:
当没有用extern "C" 包含代码时, 运行会出现下列错误。
/lib/libmax.so: undefined symbol: max
你可以用nm 来查看libmax.so, 看其输出符号到底是什么,一看,知道应该用C 名称导出。
其它常见错误为:
1.没有包含dlfcn.h 头文件, 引起编译错误
test_d.cpp: In function ‘int main(int, char**)’:
test_d.cpp:9:36: error: ‘RTLD_LAZY’ was not declared in this scope
test_d.cpp:9:45: error: ‘dlopen’ was not declared in this scope
test_d.cpp:15:34: error: ‘dlsym’ was not declared in this scope
test_d.cpp:18:21: error: ‘dlerror’ was not declared in this scope
test_d.cpp:24:16: error: ‘dlclose’ was not declared in this scope
2.连接没有包含libdl.so, 出现连接错误
$ g++ -o test_d test_d.cpp
/tmp/ccv8xKSN.o: In function `main':
test_d.cpp:(.text+0x19): undefined reference to `dlopen'
test_d.cpp:(.text+0x50): undefined reference to `dlsym'
test_d.cpp:(.text+0x60): undefined reference to `dlerror'
test_d.cpp:(.text+0xbd): undefined reference to `dlclose'
collect2: ld returned 1 exit status
3. 生成动态库没有采用-share 选项,出现连接错误
$ g++ -o libmax.so max.cpp
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
0 0
- linux 动态链接库的创建和使用--动态连接
- linux 动态链接库的创建和使用--动态连接
- linux 动态链接库的创建和使用--静态连接
- linux 动态链接的创建和使用
- linux的动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- Linux静态/动态链接库的创建和使用
- 关于MD5破解这件事
- Struts2源码阅读(一)_Struts2框架流程概述
- UNIX网络编程
- lua对象类内部是实现
- hdu1272 -- 小希的迷宫(并查集)
- linux 动态链接库的创建和使用--动态连接
- 请过来人指点迷津:国企、外企、民营,我该选哪个???
- Java 中的static关键字
- WordladderII
- Android手势处理
- Bootstrap里的文件分别表示什么?都有什么用?
- Android setColorFilter 滤镜效果
- 理解操作系统1--构建工具链过程疑惑总结
- 友盟统计分析