Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148630
  • 博文数量: 42
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 39
  • 用 户 组: 普通用户
  • 注册时间: 2016-01-07 14:06
文章分类

全部博文(42)

文章存档

2017年(1)

2016年(41)

分类: LINUX

2017-10-13 16:23:03

    先来说说静态库
   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  Programming》

    由上面可以看到,中,
    1.静态库是一些目标文件(后缀名为.o)的集合体而已。 
    2.静态库的后缀名是.a,对应于windows操作系统的后缀名为.lib的静态库。
    3.可以使用ar命令来创建一个静态库文件。
    来看一个实例,根据书中的代码简化的,先看一看可以编译成库文件的源文件中的代码:

[cpp] view plain copy
 print?
  1. /*  test.c  */  
  2. int f()   
  3. {  
  4.   return 3;  
  5. }  

代码非常简单,只有一句话。我们敲入如下命令:
gcc –c test.c
ar cr libtest.a test.o
会在当前目录下生成一个libtest.a静态库文件。-c表示只编译,不链接。再来看一看如何使用这个库。如下代码:

[cpp] view plain copy
 print?
  1. /*  app.c   */  
  2. #include   
  3. extern int f();  
  4. int main()   
  5. {  
  6.   printf(“return value is %d\n”,f());  
  7.   return 0;  
  8. }  

敲入如下命令:
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  Programming》

由上面可以看到,中,
1.和静态库类似,动态库文件也是一些目标文件(后缀名为.o)的集合体而已。
2.动态库的后缀名是.so,对应于windows操作系统的后缀名为.dll的动态库。
3.可以使用gcc命令来创建一个动态库文件。
来看一个实例,和静态库的代码实际是一样的。先看看可以编译成库文件的源文件中的代码:

[cpp] view plain copy
 print?
  1. /*  test.c  */  
  2. int f()   
  3. {  
  4.   return 3;  
  5. }  

代码非常简单。我们敲入下列命令:
gcc -c -fPIC test.c
gcc -shared -fPIC -o libtest.so test.o
会在当前目录下生成一个libtest.so动态库文件。再看如何使用这个库。看如下代码:

[cpp] view plain copy
 print?
  1. /*  app.c   */  
  2. #include   
  3. extern int f();  
  4. int main()   
  5. {  
  6.   printf(“return value is %d\n”,f());  
  7.   return 0;  
  8. }  

敲入如下命令:
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,我们发现,程序就正常运行了。


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

阅读(1741) | 评论(0) | 转发(0) |
0

上一篇:linux设备驱动归纳总结

下一篇:没有了

给主人留下些什么吧!~~