Linux Static and Dynamic Library

先来说说静态库

An archive (or static library) is simply a collection of object files stored as a single file.(An archive is roughly the equivalent of a Windows .LIB file.) When you provide an archive to the linker, the linker searches the archive for the object files it needs, extracts them, and links them into your program much as if you had provided those object files directly. 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 Linux Programming》

由上面可以看到,linux 操作系统中:

1. 静态库是一些目标文件 (后缀名为. o) 的集合体而已。

2. 静态库的后缀名是. a,对应于 windows 操作系统的后缀名为. lib 的静态库。

3. 可以使用 ar 命令来创建一个静态库文件。

来看一个实例,根据书中的代码简化的,先看一看可以编译成库文件的源文件中的代码:


/*  test.c  */
int f()   
{
    return 3;
}

代码非常简单,只有一句话。我们敲入如下命令:


gcc –c test.c ar cr libtest.a test.o

会在当前目录下生成一个 libtest.a 静态库文件。-c 表示只编译,不链接。再来看一看如何使用这个库。如下代码:


/*  app.c   */
#include 
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 的可执行文件。-o 表示指定输出文件名。执行一下./app,可以看一看结果:

这就是生成 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 
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.so 文件和生成的 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,那么执行命令


exportLD_LIBRARY_PATH=/root/Desktop/kontronn

后,再执行./app,我们发现,程序就正常运行了。

还有一点要注意的,如果一个库的动态库和静态库同时存在时,默认是要先找动态库的。