跳至主要内容

【转】详细讲解 关于Linux静态库和动态库的分析

基本概念

库有动态与静态两种,动态通常用.so为后缀,静态用.a为后缀。

例如:libhello.so libhello.a 为了在同一系统中使用不同版本的库,可以在库文件名后加上版本号为后缀,例如: libhello.so.1.0,由于程序连接默认以.so为文件后缀名。所以为了使用这些库,通常使用建立符号连接的方式。

ln -s libhello.so.1.0 libhello.so.1

ln -s libhello.so.1 libhello.so


1、使用库

当要使用静态的程序库时,连接器会找出程序所需的函数,然后将它们拷贝到执行文件,由于这种拷贝是完整的,所以一旦连接成功,静态程序库也就不再需要了。然 而,对动态库而言,就不是这样。动态库会在执行程序内留下一个标记指明当程序执行时,首先必须载入这个库。由于动态库节省空间,Linux下进行连接的缺省操作是首先连接动态库,也就是说,如果同时存在静态和动态库,不特别指定的话,将与动态库相连接。 现在假设有一个叫hello的程序开发包,它提供一个静态库libhello.a 一个动态库libhello.so,一个头文件hello.h,头文件中提供sayhello()这个函数 /* hello.h */ void sayhello(); 另外还有一些说明文档。

这一个典型的程序开发包结构 与动态库连接 Linux默认的就是与动态库连接,下面这段程序testlib.c使用hello库中的sayhello()函数

/*testlib.c*/

#include <>

#include <>

int main()

{

   sayhello();

   return 0;

}

使用如下命令进行编译 $gcc -c testlib.c -o testlib.o

用如下命令连接: $gcc testlib.o -lhello -o testlib

连接时要注意,假设libhello.o 和libhello.a都在缺省的库搜索路径下/usr/lib下,如果在其它位置要加上-L参数 与与静态库连接麻烦一些,主要是参数问题。还是上面的例子:

$gcc testlib.o -o testlib -WI,-Bstatic -lhello

注:这个特别的"-WI,-Bstatic"参数,实际上是传给了连接器ld。指示它与静态库连接,如果系统中只有静态库当然就不需要这个参数了。 如果要和多个库相连接,而每个库的连接方式不一样,比如上面的程序既要和libhello进行静态连接,又要和libbye进行动态连接,其命令应为:

$gcc testlib.o -o testlib -WI,-Bstatic -lhello -WI,-Bdynamic -lbye


 

2、动态库的路径问题 为了让执行程序顺利找到动态库,有三种方法:

(1)把库拷贝到/usr/lib和/lib目录下。

(2)在LD_LIBRARY_PATH环境变量中加上库所在路径。

例如动态库libhello.so在/home/ting/lib目录下,以bash为例,使用命令:

$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/ting/lib

(3) 修改/etc/ld.so.conf文件,把库所在的路径加到文件末尾,并执行ldconfig刷新。这样,加入的目录下的所有库文件都可见。


3、查看库中的符号

有时候可能需要查看一个库中到底有哪些函数,nm命令可以打印出库中的涉及到的所有符号。库既可以是静态的也可以是动态的。nm列出的符号有很多,常见的有三种:

一种是在库中被调用,但并没有在库中定义(表明需要其他库支持),用U表示;

一种是库中定义的函数,用T表示,这是最常见的;

另外一种是所谓的"弱 态"符号,它们虽然在库中被定义,但是可能被其他库中的同名符号覆盖,用W表示。

例如,假设开发者希望知道上文提到的hello库中是否定义了 printf():

$nm libhello.so |grep printf U

其中printf U表示符号printf被引用,但是并没有在函数内定义,由此可以推断,要正常使用hello库,必须有其它库支持,再使用ldd命令查看hello依赖于哪些库:

$ldd hello libc.so.6=>/lib/libc.so.6(0x400la000) /lib/ld-Linux.so.2=>/lib/ld-Linux.so.2 (0x40000000)

从上面的结果可以继续查看printf最终在哪里被定义,有兴趣可以go on


4、生成库

第一步要把源代码编绎成目标代码。

以下面的代码为例,生成上面用到的hello库:

 /* hello.c */

#include <> 

void sayhello()

{

  printf("hello,world ");

}

用gcc编绎该文件,在编绎时可以使用任何全法的编绎参数,例如-g加入调试代码等: gcc -c hello.c -o hello.o

(1)连接成静态库 连接成静态库使用ar命令,其实ar是archive的意思

 $ar cqs libhello.a hello.o

(2)连接成动态库 生成动态库用gcc来完成,由于可能存在多个版本,因此通常指定版本号:

$gcc -shared -Wl,-soname,libhello.so.1 -o libhello.so.1.0 hello.o

另外再建立两个符号连接:

$ln -s libhello.so.1.0 libhello.so.1

$ln -s libhello.so.1 libhello.so

这样一个libhello的动态连接库就生成了。最重要的是传gcc -shared 参数使其生成是动态库而不是普通执行程序。 -Wl 表示后面的参数也就是-soname,libhello.so.1直接传给连接器ld进行处理。实际上,每一个库都有一个soname,当连接器发现它正在查找的程序库中有这样一个名称,连接器便会将soname嵌入连结中的二进制文件内,而不是它正在运行的实际文件名,在程序执行期间,程序会查找拥有 soname名字的文件,而不是库的文件名,换句话说,soname是库的区分标志。 这样做的目的主要是允许系统中多个版本的库文件共存,习惯上在命名库文件的时候通常与soname相同 libxxxx.so.major.minor 其中,xxxx是库的名字,major是主版本号,minor 是次版本号


总结

通过对Linux库工作的分析,我们已经可以理解程序运行时如何去别的地方寻找"库",在下一篇文章中我继续研究可执行程序的执行过程,这两天在写一个服务器上的脚本,快成功了。花在Linux上的时间明显少了点,等过两天把这个小程序开发完毕以后马上转回正行。

评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】tcphdr结构详解

位于:/usr/src/linux/include/linux/tcp.h struct tcphdr { __be16 source; __be16 dest; __be32 seq; __be32 ack_seq; #if defined(__LITTLE_ENDIAN_BITFIELD) __u16   res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; #elif defined(__BIG_ENDIAN_BITFIELD) __u16   doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; #else #error "Adjust your <asm/byteorder.h> defines"