Linux之ldd命令
简介
在linux中, ldd是list, dynamic, dependencies的缩写, 意思是, 列出动态库依赖关系。ldd本身不是一个程序,而仅是一个shell脚本:ldd可以列出一个程序所需要得动态链接库(so)。
1 2 3 4 5 6 7 8 9 10 11 | [root@docker35 ~]# ldd --help Usage: ldd [OPTION]... FILE... --help print this help and exit --version print version information and exit -d, --data-relocs process data relocations -r, --function-relocs process data and function relocations -u, --unused print unused direct dependencies -v, --verbose print all information For bug reporting instructions, please see: <http://www.gnu.org/software/libc/bugs.html>. |
参数说明:
--version 打印ldd的版本号
-v --verbose 打印所有信息,例如包括符号的版本信息
-d --data-relocs 执行符号重部署,并报告缺少的目标对象(只对ELF格式适用)
-r --function-relocs 对目标对象和函数执行重新部署,并报告缺少的目标对象和函数(只对ELF格式适用)
--help 用法信息
当然, 你也可以用ldd --help或者man ldd来看其用法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | [root@docker35 ~]# which ldd /usr/bin/ldd [root@docker35 ~]# file /usr/bin/ldd /usr/bin/ldd: Bourne-Again shell script, ASCII text executable [root@docker35 ~]# more /usr/bin/ldd #! /usr/bin/bash # Copyright (C) 1996-2011, 2012 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # The GNU C Library 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 # Lesser General Public License for more details. # You should have received a copy of the GNU Lesser General Public # License along with the GNU C Library; if not, see # <http://www.gnu.org/licenses/>. # This is the `ldd' command, which lists what shared libraries are # used by given dynamically-linked executables. It works by invoking the # run-time dynamic linker as a command and setting the environment # variable LD_TRACE_LOADED_OBJECTS to a non-empty value. # We should be able to find the translation right at the beginning. |
在 ldd 命令打印的结果中,“=>”左边的表示该程序需要连接的共享库之 so 名称,右边表示由 Linux 的共享库系统找到的对应的共享库在文件系统中的具体位置。默认情况下, /etc/ld.so.conf 文件中包含有默认的共享库搜索路径。
1 2 3 4 5 6 7 8 9 10 11 | [root@docker35 ~]# ldd /bin/ls linux-vdso.so.1 => (0x00007fff0c16f000) libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f4320c39000) libcap.so.2 => /lib64/libcap.so.2 (0x00007f4320a34000) libacl.so.1 => /lib64/libacl.so.1 (0x00007f432082b000) libc.so.6 => /lib64/libc.so.6 (0x00007f432045d000) libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f43201fb000) libdl.so.2 => /lib64/libdl.so.2 (0x00007f431fff7000) /lib64/ld-linux-x86-64.so.2 (0x00007f4320e60000) libattr.so.1 => /lib64/libattr.so.1 (0x00007f431fdf2000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f431fbd6000) |
总结
1、如果使用 ldd 命令时没有找到对应的共享库文件和其具体位置,可能是两种情况引起的:
a、共享库没有安装在该系统中;
b、共享库保存在 /etc/ld.so.conf 文件列出的搜索路径之外的位置。
通常情况下,许多开放源代码的程序或函数库都会默认将自己安装到 /usr/local 目录下的相应位置(如:/usr/local/bin 或 /usr/local/lib),以便与系统自身的程序或函数库相区别。而许多 Linux 系统的 /etc/ld.so.conf 文件中默认又不包含 /usr/local/lib。因此,往往会出现已经安装了共享库,但是却无法找到共享库的情况。具体解决办法如下:
检查 /etc/ld.so.conf 文件,如果其中缺少 /usr/local/lib 目录,就添加进去;注意:在修改了 /etc/ld.so.conf文件或者在系统中安装了新的函数库之后,需要运行一个命令:ldconfig ,该命令用来刷新系统的共享库缓存,即 /etc/ld.so.cache 文件。为了减少共享库系统的库搜索时间,共享库系统维护了一个共享库 so 名称的缓存文件/etc/ld.so.cache。因此,在安装新的共享库之后,一定要运行 ldconfig 刷新该缓存。
2、使用nm命令也可以获取库函数的信息;nm命令可以列出一个函数库文件中的符号表,它对静态的库函数和共享的库函数都能起作用。