13

我是 Linux 新手(来自 Windows 背景)。我想了解在运行时如何加载共享对象(.so 文件)的详细信息。根据http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html文件 /etc/ld.so.conf 配置目录以在运行时搜索 .so 文件。然而,下面显示的实验似乎表明情况并非如此。/usr/local/lib 在 /etc/ld.so.conf.d 中的 .conf 文件之一中,但是当我的 .so 在 /usr/local/lib 中时,在运行时找不到它。相反,/etc/ld.so.conf没有配置 /usr/lib,但是当我的 .so 在 /usr/lib 中时,它在运行时找到。

我错过了什么?

谢谢,戴夫

davids@ds-ub64-7:/$ # Display the .so search path configured in /etc/ld.so.conf
davids@ds-ub64-7:/$ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf
davids@ds-ub64-7:/$ cat /etc/ld.so.conf.d/*.conf
/usr/lib/mesa
/usr/lib32/mesa
/usr/lib32/alsa-lib
/usr/lib/alsa-lib
# libc default configuration
/usr/local/lib
# Multiarch support
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu

davids@ds-ub64-7:/$ # libsotest.so.1 is in /usr/local/lib
davids@ds-ub64-7:/$ ls -la /usr/local/lib/libsotest*
lrwxrwxrwx 1 root   root     14 2012-07-19 08:24 /usr/local/lib/libsotest.so -> libsotest.so.1
lrwxrwxrwx 1 root   root     18 2012-07-19 08:24 /usr/local/lib/libsotest.so.1 ->     libsotest.so.1.0.1
-rwxr-xr-x 1 davids davids 7952 2012-07-19 08:13 /usr/local/lib/libsotest.so.1.0.1

davids@ds-ub64-7:/$ # But when I run an executable that refrs to libsotest.so.1, the loader     doesn't find it.
davids@ds-ub64-7:/$ /projects/sotest/exe/sotestexe
/projects/sotest/exe/sotestexe: error while loading shared libraries: libsotest.so.1: cannot     open shared object file: No such file or directory

davids@ds-ub64-7:/$ # Configure loader to display the paths it's searching...it's     searching /usr/lib but not /usr/local/lib
davids@ds-ub64-7:/$ export LD_DEBUG=lib
davids@ds-ub64-7:/$ /projects/sotest/exe/sotestexe
warning: debug option `lib' unknown; try LD_DEBUG=help
/projects/sotest/exe/sotestexe: error while loading shared libraries: libsotest.so.1: cannot     open shared object file: No such file or directory
davids@ds-ub64-7:/$ export LD_DEBUG=libs
davids@ds-ub64-7:/$ /projects/sotest/exe/sotestexe
      6691: find library=libsotest.so.1 [0]; searching
      6691:  search cache=/etc/ld.so.cache
      6691:  search     path=/lib/tls/x86_64:/lib/tls:/lib/x86_64:/lib:/usr/lib/tls/x86_64:/usr/lib/tls:/usr/lib/x86_64:/usr/    lib:/lib/x86_64-linux-gnu/tls/x86_64:/lib/x86_64-linux-gnu/tls:/lib/x86_64-linux-    gnu/x86_64:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu/tls/x86_64:/usr/lib/x86_64-linux-    gnu/tls:/usr/lib/x86_64-linux-gnu/x86_64:/usr/lib/x86_64-linux-gnu        (system search path)
      6691:   trying file=/lib/tls/x86_64/libsotest.so.1
      6691:   trying file=/lib/tls/libsotest.so.1
      6691:   trying file=/lib/x86_64/libsotest.so.1
      6691:   trying file=/lib/libsotest.so.1
      6691:   trying file=/usr/lib/tls/x86_64/libsotest.so.1
      6691:   trying file=/usr/lib/tls/libsotest.so.1
      6691:   trying file=/usr/lib/x86_64/libsotest.so.1
      6691:   trying file=/usr/lib/libsotest.so.1
      6691:   trying file=/lib/x86_64-linux-gnu/tls/x86_64/libsotest.so.1
      6691:   trying file=/lib/x86_64-linux-gnu/tls/libsotest.so.1
      6691:   trying file=/lib/x86_64-linux-gnu/x86_64/libsotest.so.1
      6691:   trying file=/lib/x86_64-linux-gnu/libsotest.so.1
      6691:   trying file=/usr/lib/x86_64-linux-gnu/tls/x86_64/libsotest.so.1
      6691:   trying file=/usr/lib/x86_64-linux-gnu/tls/libsotest.so.1
      6691:   trying file=/usr/lib/x86_64-linux-gnu/x86_64/libsotest.so.1
      6691:   trying file=/usr/lib/x86_64-linux-gnu/libsotest.so.1
      6691: 
/projects/sotest/exe/sotestexe: error while loading shared libraries: libsotest.so.1: cannot     open shared object file: No such file or directory
davids@ds-ub64-7:/$
4

1 回答 1

15

/usr/lib/lib是硬编码的 IIRC。

在ld.conf.so中添加 PATH是不够的,您必须使用ldconfig重新生成缓存和链接。

您可以使用 . 检查缓存中的库ldconfig -p

于 2012-07-19T13:00:00.100 回答