函式库管理:ldconfig 与 /etc/ld.so.conf 以及 ldd

本文介绍如何通过将常用动态库加载到内存缓存中来提高Linux系统的动态库读取性能。涉及配置文件ld.so.conf的修改及ldconfig命令的使用,并通过示例展示了如何检查动态库依赖。

现在知道了动态与静态函数库,也知道了当前的Linux大多是将函数库做成动态函数库,下面来讨论增加函数库读取性能的方法。我们知道,内存的访问速度是硬盘的好几倍,所以,如果将常用的动态函数库加载到内存中(高速缓存,cache),当软件套件要采用动态函数库时,就不需要重新从硬盘里读出,这样就可以提高动态函数库的读取速度。这个时候需要ldconfig/etc/ld.so.conf的帮助。

将动态函数库加载到高速缓存(cache)中的过程如下:

1. 首先,要在 /etc/ld.so.conf中写下“想要读入高速缓存中的动态函数库所在的目录”,注意,是目录而不是文件。

2. 利用ldconfig执行文件将 /etc/ld.so.conf的数据读入高速缓存中。

3. 同时在 /etc/ld.so.cache文件中记录数据。

事实上,ldconfig还可以用来判断动态函数库的连接信息。赶紧使用Fedora Core 4来测试吧。假设要将MySQL函数库加入到高速缓存中:

 

透过上面的动作,我们可以将 MySQL 的相关函式库给他读入快取当中, 这样可以加快函式库读取的效率呢!在某些时候,您可能会自行加入某些 Tarball 安装的动态函式库,而您想要让这些动态函式库的相关连结可以被读入到快取当中, 这个时候您可以将动态函式库所在的目录名称写入 /etc/ld.so.conf 当中,然后执行 ldconfig 就可以啦!

ldd:
     说了这么多,那么我如何判断某个可执行的 binary 档案含有什么动态函式库呢?很简单,利用 ldd 就可以晓得了!例如我想要知道 /usr/bin/passwd 这个程式含有的动态函式库有哪些,可以这样做:
      

未来如果您常常升级安装 RPM 的套件时( 下一章节会介绍 ),应该常常会发现那个‘ 相依属性’的问题吧!?没错!我们可以先以 ldd 来视察‘相依函式库’之间的相关性!以先取得瞭解! 例如上面的例子中,我们检查了 libc.so.6 这个在 /lib 当中的函式库,结果发现他其实还跟 ld-linux.so.2 有关!所以我们就需要来瞭解一下,那个档案到底是什么套件的函式库呀!?使用 -v 这个参数还可以得知该函式库来自于哪一个套件!像上面的资料中,就可以得到该 libc.so.6 其实可以支援 GLIBC_2.1 等的版本!

 

root@ubuntu:/zlab_16t2/changqingteng# echo "/usr/local/lib" | tee /etc/ld.so.conf.d/freesasa.conf /usr/local/lib root@ubuntu:/zlab_16t2/changqingteng# ldconfig -v | grep freesasa /sbin/ldconfig.real: Can't stat /usr/local/lib/i386-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/i386-linux-gnu' given more than once (from /etc/ld.so.conf.d/i386-linux-gnu.conf:4 and /etc/ld.so.conf.d/i386-linux-gnu.conf:3) /sbin/ldconfig.real: Can't stat /usr/local/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Can't stat /usr/lib/i686-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/local/lib' given more than once (from /etc/ld.so.conf.d/libc.conf:2 and /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: Can't stat /usr/local/lib/x86_64-linux-gnu: No such file or directory /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from /etc/ld.so.conf.d/x86_64-linux-gnu.conf:4 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib32' given more than once (from /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:3 and /etc/ld.so.conf.d/zz_i386-biarch-compat.conf:2) /sbin/ldconfig.real: Path `/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once (from <builtin>:0 and /etc/ld.so.conf.d/x86_64-linux-gnu.conf:3) /sbin/ldconfig.real: Path `/usr/lib' given more than once (from <builtin>:0 and <builtin>:0) /sbin/ldconfig.real: /lib/i386-linux-gnu/ld-linux.so.2 is the dynamic linker, ignoring /usr/local/lib: (from /etc/ld.so.conf.d/freesasa.conf:1) /sbin/ldconfig.real: /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 is the dynamic linker, ignoring /sbin/ldconfig.real: /lib32/ld-linux.so.2 is the dynamic linker, ignoring 请分析错误原因,并给出详细解决步骤
07-27
[root@VM-24-11-opencloudos nginx-1.24.0]# export LUAJIT_LIB=/usr/local/lib [root@VM-24-11-opencloudos nginx-1.24.0]# ls -l ${LUAJIT_LIB}/libluajit-5.1.so* lrwxrwxrwx 1 root root 33 Jun 18 11:12 /usr/local/lib/libluajit-5.1.so -> /usr/local/lib/libluajit-5.1.so.2 lrwxrwxrwx 1 root root 31 Jun 17 16:40 /usr/local/lib/libluajit-5.1.so.2 -> libluajit-5.1.so.2.1.1748495995 -rwxr-xr-x 1 root root 3135760 Jun 17 14:25 /usr/local/lib/libluajit-5.1.so.2.1.0 -rwxr-xr-x 1 root root 3148872 Jun 17 16:40 /usr/local/lib/libluajit-5.1.so.2.1.1748495995 [root@VM-24-11-opencloudos nginx-1.24.0]# export LUAJIT_INC=/usr/local/include/luajit-2.1 [root@VM-24-11-opencloudos nginx-1.24.0]# ls -l ${LUAJIT_INC}/luajit.h -rw-r--r-- 1 root root 3011 Jun 17 16:40 /usr/local/include/luajit-2.1/luajit.h [root@VM-24-11-opencloudos nginx-1.24.0]# ls ${LUAJIT_LIB}/libluajit-5.1.so* /usr/local/lib/libluajit-5.1.so /usr/local/lib/libluajit-5.1.so.2.1.0 /usr/local/lib/libluajit-5.1.so.2 /usr/local/lib/libluajit-5.1.so.2.1.1748495995 [root@VM-24-11-opencloudos nginx-1.24.0]# sudo ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/libluaj it-5.1.so.2 [root@VM-24-11-opencloudos nginx-1.24.0]# sudo ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/local/lib64/libluajit-5.1.so.2 [root@VM-24-11-opencloudos nginx-1.24.0]# echo "/usr/local/lib" | sudo tee /etc/ld.so.conf.d/luajit.conf /usr/local/lib [root@VM-24-11-opencloudos nginx-1.24.0]# echo "/usr/local/lib64" | sudo tee -a /etc/ld.so.conf.d/luajit.conf /usr/local/lib64 [root@VM-24-11-opencloudos nginx-1.24.0]# sudo ldconfig -v | grep luajit # 应显示库路径 ldconfig: Path `/usr/local/lib' given more than once (from /etc/ld.so.conf:2 and /etc/ld.so.conf.d/luajit.conf:1) ldconfig: Can't stat /libx32: No such file or directory ldconfig: Path `/usr/lib' given more than once (from <builtin>:0 and <builtin>:0) ldconfig: Path `/usr/lib64' given more than once (from <builtin>:0 and <builtin>:0) ldconfig: Can't stat /usr/libx32: No such file or directory /usr/local/lib: (from /etc/ld.so.conf.d/luajit.conf:1) libluajit-5.1.so.2 -> libluajit-5.1.so.2.1.1748495995 /usr/local/lib64: (from /etc/ld.so.conf.d/luajit.conf:2)
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值