linux下ldconfig命令到底是在干什么

首先,从命令本身来看,顾名思义,ldconfig \Rightarrow ld   config   即load configure 的所写

那么系统中那么多配置,这里load的是哪一项configure呢?

于是我们在终端输入命令

man ldconfig

得到如下说明

LDCONFIG(8)                Linux Programmer's Manual               LDCONFIG(8)

NAME
       ldconfig - configure dynamic linker run-time bindings

SYNOPSIS
       /sbin/ldconfig [-nNvXV] [-f conf] [-C cache] [-r root] directory...
       /sbin/ldconfig -l [-v] library...
       /sbin/ldconfig -p

DESCRIPTION
       ldconfig  creates  the  necessary  links  and  cache to the most recent
       shared libraries found in the  directories  specified  on  the  command
       line, in the file /etc/ld.so.conf, and in the trusted directories, /lib
       and /usr/lib (on some 64-bit architectures  such  as  x86-64,  lib  and
       /usr/lib are the trusted directories for 32-bit libraries, while /lib64
       and /usr/lib64 are used for 64-bit libraries).

       The cache is used by the run-time linker, ld.so or ld-linux.so.  ldcon‐
       fig checks the header and filenames of the libraries it encounters when
       determining which versions should have their links updated.

       ldconfig will attempt to deduce the type of ELF libraries (i.e.,  libc5
       or  libc6/glibc)  based  on  what  C libraries, if any, the library was
       linked against.

       Some existing libraries do not contain enough information to allow  the
       deduction  of  their  type.  Therefore, the /etc/ld.so.conf file format
       allows the specification of an expected type.  This is  used  only  for
       those  ELF  libraries  which  we  can  not  work  out.   The  format is
       "dirname=TYPE", where TYPE can be libc4, libc5, or libc6.  (This syntax
       also works on the command line.)  Spaces are not allowed.  Also see the
       -p option.  ldconfig should normally be run by the superuser as it  may
       require write permission on some root owned directories and files.

简单的说,ldconfig创建了一个指向特定目录的链接,当程序需要使用动态库时就会使用这个链接去寻找动态库【动态库的概念先按下不说】。这个链接指定的目录有/lib, /usr/lib 【不同架构机器的目录名会略有不同】,还有/etc/ld.so.conf 文件中的目录。

到此,我们知道了,当我们的动态库更新时就需要使用ldconfig命令来更新这个链接,当然也就是说如果你要添加新的动态库文件,那你或者把这些库放到已有的库目录中,或者将新建的库目录添加到/etc/ld.so.conf中。


                                                              扩展


有了以上的描述,我们算是整体把握了ldconfig这个命令,下面我们把之前按下的动态库拿出来捣鼓捣鼓

所谓动态库即动态链接库是库文件的一种,库文件可以简单的看成一种代码仓库,它提供给使用者一些可以直接拿来用的变量、函数或类。与动态库相呼应的自然还有一个叫静态库。(下面的内容我们是以linux系统作为讨论对象,其他系统与之大同小异)

  • 静态库的特点如下:
扩展名(扩展名为 .a) 静态库通常扩展名为 libxxx.a ;
编译行为:静态库在编译的时候会直接整合到执行程序当中,所以利用静态库编译成的目标会比较大一些;
独立执行的状态:静态库最大的优点,就是编译成功的可执行文件可以独立执行,而不需要再向外部要求读取库的内容;
升级难易度:虽然执行程序可以独立执行,但因为静态库是直接整合到目标中, 因此若库升级时,整个执行程序必须要重新编译才能将新版的库整合到程序当中。 也就是说,在升级方面,只要库升级了,所有将此库纳入的程序都需要重新编译!
  • 动态库的特点如下:
扩展名:(扩展名为 .so) 这类库通常扩展名为 libxxx.so 的类型;
编译行为:动态库与静态库的编译行为差异挺大的。 与静态库被整合到程序中不同,动态库在编译的时候,在程序里面只有一个指向动态库位置的指针而已。也就是说,动态库的内容并没有被整合到执行程序当中,而是当执行程序要使用到库时, 程序才会去读取动态库来使用。由于执行程序中仅具有指向库所在的指针而已, 并不包含库的内容,所以它的文件会比较小一点;
独立执行的状态:动态库所编译出来的程序不能被独立执行, 因为当我们使用到库时,程序才会去读取库,所以库文件『必须要存在』才行,而且,库文件的『所在目录也不能改变』,因为我们的可执行文件里面仅有指针,当需要库文件的内容时, 程序会主动去指针指向的路径读取,所以动态库不能随意移动或删除,否则会影响到对此有依赖的软件;
升级难易度:虽然这类型的执行档无法独立运作,然而由于是具有指向的功能, 所以,当库升级后,执行程序根本不需要进行重新编译,因为执行程序会直接指向新的库文件 (前提是库文件新旧版本的名字要相同喔!)。

 

##reference

1. https://www.cnblogs.com/schips/p/10183111.html

2. 鸟哥的linux私房菜基础篇

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值