winston 发表于 2011-12-26 21:47:59

Linux环境下动态库的生成和使用

http://hi.csdn.net/attachment/201112/25/0_1324819418jMot.gif前面说了linux下的静态库。现在来说一说linux下的动态库。

   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命令,来执行这个可执行文件时,却提示如下错误:
http://hi.csdn.net/attachment/201112/25/0_1324819418jMot.gif
这就奇怪了,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,我们发现,程序就正常运行了。作者:haojiahuo50401 发表于2011-12-25 21:29:19 原文链接
页: [1]
查看完整版本: Linux环境下动态库的生成和使用