Linux环境下动态库的生成和使用 (.so文件)
来源:互联网 发布:互联网运营 知乎 编辑:程序博客网 时间:2024/05/30 07:11
A shared library (also known as a shared object, or as a dynamically linked library) is similar to a archive in that it is a grouping of object files. However, there are many important differences.The most fundamental difference is that when a shared library is linked into a program, the final executable does not actually contain the code that is present in the shared library. Instead, the executable merely contains a reference to the shared library. If several programs on the system are linked against the same shared library, they will all reference the library, but none will actually be included.Thus, the library is “shared” among all the programs that link with it.
A second important difference is that a shared library is not merely a collection of object files, out of which the linker chooses those that are needed to satisfy undefined references. Instead, the object files that compose the shared library are combined into a single object file so that a program that links against a shared library always includes all of the code in the library, rather than just those portions that are needed.
To create a shared library, you must compile the objects that will make up the library using the -fPIC option to the compiler, like this:
% gcc -c -fPIC test1.c
The -fPIC option tells the compiler that you are going to be using test.o as part of a shared object.
---摘自《Advanced Linux Programming》
由上面可以看到,linux操作系统中,
1.和静态库类似,动态库文件也是一些目标文件(后缀名为.o)的集合体而已。
2.动态库的后缀名是.so,对应于windows操作系统的后缀名为.dll的动态库。
3.可以使用gcc命令来创建一个动态库文件。
来看一个实例,和静态库的代码实际是一样的。先看看可以编译成库文件的源文件中的代码:
/*test.c*/int f() { return 3;}
代码非常简单。我们敲入下列命令:
gcc -c -fPIC test.c
gcc -shared -fPIC -o libtest.so test.o
会在当前目录下生成一个libtest.so动态库文件。再看如何使用这个库。看如下代码:
/*app.c*/#include <stdio.h>extern int f();int main() { printf(“return value is %d\n”,f()); return 0;}
敲入如下命令:
gcc –c app.c
gcc -o app app.o -L. –ltest
敲命令的时候要记得将libtest.a文件和生成的app.o文件放在同一个目录(即当前目录)下。这样,敲入命令后,会在当前目录下生成一个名为app的可执行文件。但是当我们执行./app命令,来执行这个可执行文件时,却提示如下错误:
这就奇怪了,libtest.so文件明明就在当前目录下,为什么会提示找不到呢?继续看书,书上有这样的话:
When you link a program with a shared library, the linker does not put the full path to the shared library in the resulting executable. Instead, it places only the name of the shared library.When the program is actually run, the system searches for the shared library and loads it.The system searches only /lib and /usr/lib, by default.If a shared library that is linked into your program is installed outside those directories, it will not be found, and the system will refuse to run the program.
原来linux和windows的机制是不同的,它不会在当前目录下寻找动态连接库文件,它只会在标准路径下寻找。(The system searches only /lib and /usr/lib, by default.)。我们可以使用一个命令,使得操作系统去我们指定的路径下面去寻找。假设libtest.so文件所在的目录是/root/Desktop/aabb,那么执行命令
export LD_LIBRARY_PATH=/root/Desktop/aabb
后,再执行./app,我们发现,程序就正常运行了。
- Linux环境下动态库的生成和使用 (.so文件)
- Linux环境下动态库的生成和使用 (.so文件)
- Linux环境下静态库和动态库的生成和使用 (.a文件和.so文件)
- Linux 下 动态链接库 .so 的生成和使用
- Linux 下使用gcc生成so(动态链接库)文件
- Linux下so文件的生成和使用
- Linux下动态库so的生成与使用
- Linux下动态库so的生成与使用
- Linux下动态链接库so的生成与使用
- Linux下动态库so的生成与使用
- Linux下动态库(*.so)的生成和使用 [2014-01-02更新]
- Linux下的动态链接库.so文件的使用
- Linux下的动态链接库.so文件的使用
- Linux下的动态链接库.so文件的使用
- Linux下的动态链接库.so文件的使用
- C++文件如何在linux下生成动态库So,以及如何使用这个动态库
- C++文件如何在linux下生成动态库So,以及如何使用这个动态库
- C++文件如何在linux下生成动态库So,以及如何使用这个动态库
- 循环队列的实现
- vim的入门
- 解读Content Provider之二
- 基于linux 的socket UDP编程例程
- 申请Android开发者账号
- Linux环境下动态库的生成和使用 (.so文件)
- myeclipse8.5 ,An internal error occurred during: "Add Deployment".解决办法
- jsp Bean 标签 使用 一般规则 一个例子
- 1114
- Linux安全体系学习笔记之四:OpenSSL源代码分析(3)
- 【数据结构】图的实现
- 一个安静的地方
- Crack之初体验-第三课
- 号外!号外!……