分类: LINUX
2017-09-24 12:06:50
原文地址:linux 动态库与静态库的生成与使用 作者:chinaguounix
You can create an archive using the ar command.Archive files traditionally use a .a extension rather than the .o extension used by ordinary object files. Here’ s how you would combine test1.o and test2.o into a single libtest.a archive:
% ar cr libtest.a test1.o test2.o
The cr flags tell ar to create the archive.
---摘自《Advanced Programming》
由上面可以看到,中,
1.静态库是一些目标文件(后缀名为.o)的集合体而已。
2.静态库的后缀名是.a,对应于windows操作系统的后缀名为.lib的静态库。
3.可以使用ar命令来创建一个静态库文件。
来看一个实例,根据书中的代码简化的,先看一看可以编译成库文件的源文件中的代码:
代码非常简单,只有一句话。我们敲入如下命令:
gcc –c test.c
ar cr libtest.a test.o
会在当前目录下生成一个libtest.a静态库文件。-c表示只编译,不链接。再来看一看如何使用这个库。如下代码:
敲入如下命令:
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 Programming》
由上面可以看到,中,
代码非常简单。我们敲入下列命令:
敲入如下命令:
gcc –c app.c
gcc -o app app.o -L. –ltest
敲命令的时候要记得将libtest.a文件和生成的app.o文件放在同一个目录(即当前目录)下。这样,敲入命令后,会在当前目录下生成一个名为app的可执行文件。-o表示指定输出文件名。执行一下./app,可以看一看结果:
这就是生成linux下面静态库的简单用法了。
再来说说动态库
1.和静态库类似,动态库文件也是一些目标文件(后缀名为.o)的集合体而已。
2.动态库的后缀名是.so,对应于windows操作系统的后缀名为.dll的动态库。
3.可以使用gcc命令来创建一个动态库文件。
来看一个实例,和静态库的代码实际是一样的。先看看可以编译成库文件的源文件中的代码:
gcc -c -fPIC test.c
gcc -shared -fPIC -o libtest.so test.o
会在当前目录下生成一个libtest.so动态库文件。再看如何使用这个库。看如下代码:
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,我们发现,程序就正常运行了。
还有一点要注意的,如果一个库的动态库和静态库同时存在时,默认是要先找动态库的。