/usr/local/lib被用来搜索共享库吗?

69

/usr/local/lib被搜索用于共享库吗?我遇到了以下错误:

[Leo@chessman ~]$ whereis ffmpeg
ffmpeg: /usr/local/bin/ffmpeg
[Leo@chessman ~]$ ffmpeg
ffmpeg: error while loading shared libraries: libavcore.so.0: cannot open shared object file: No such file or directory
[Leo@chessman ~]$ ls /usr/local/lib/libav*
/usr/local/lib/libavcodec.a            /usr/local/lib/libavfilter.a
/usr/local/lib/libavcodec.so           /usr/local/lib/libavfilter.so
/usr/local/lib/libavcodec.so.52        /usr/local/lib/libavfilter.so.1
/usr/local/lib/libavcodec.so.52.108.0  /usr/local/lib/libavfilter.so.1.74.0
/usr/local/lib/libavcore.a             /usr/local/lib/libavformat.a
/usr/local/lib/libavcore.so            /usr/local/lib/libavformat.so
/usr/local/lib/libavcore.so.0          /usr/local/lib/libavformat.so.52
/usr/local/lib/libavcore.so.0.16.1     /usr/local/lib/libavformat.so.52.94.0
/usr/local/lib/libavdevice.a           /usr/local/lib/libavutil.a
/usr/local/lib/libavdevice.so          /usr/local/lib/libavutil.so
/usr/local/lib/libavdevice.so.52       /usr/local/lib/libavutil.so.50
/usr/local/lib/libavdevice.so.52.2.3   /usr/local/lib/libavutil.so.50.36.0
[Leo@chessman ~]$ 
6个回答

82

确保你的LD_LIBRARY_PATH设置包括你想搜索的所有目录,然后再次测试它。

你可以通过以下方式快速测试:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib ffmpeg

这将仅为此次调用设置它。

或者,您可以编辑包含默认搜索目录的/etc/ld.so.conf文件。一些Linux发行版可能不会在该文件中包含/usr/local/lib

请注意,您可能还需要通过运行ldconfig(作为根用户或使用sudo)来更新缓存/etc/ld.so.cache


9
太好了,@paxdiablo的解决方案非常棒!export LD_LIBRARY_PATH=/usr/local/lib/我刚刚花了几个小时寻找这个解决方案。在我启用X264的Ubuntu 10.10上表现良好。 - DocWiki

21

来自http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html:

GNU标准建议在分发源代码时默认将所有库安装在/usr/local/lib中(所有命令应放在/usr/local/bin中)。

...

The list of directories that need to be searched is stored in the file /etc/ld.so.conf. Many distributions derived from Red Hat do not include /usr/local/lib in this file by default. This is considered a bug, and adding /usr/local/lib to /etc/ld.so.conf is a common solution for running many programs on Red Hat-derived systems. On Debian, /etc/ld.so.conf contains "include /etc/ld.so.conf.d/*.conf", and /etc/ld.so.conf.d/libc.conf contains...
# libc default configuration
/usr/local/lib

14

是和否

程序内置(或者说链接)了它们的库被找到的位置。如果一个程序期望在/usr/local/lib中找到它的库,那么它就会去找。

还有一个叫做ldconfig的程序和一个名为/etc/ld.so.conf的配置文件,很可能还有一个/etc/ld.so.conf.d目录,这些用于指定特定站点的目录。

请阅读"man ld.so",其中列出了其他开关,如环境变量LD_LIBRARY_PATH

LD.SO(8)                   Linux Programmer’s Manual                  LD.SO(8)

NAME
       ld.so, ld-linux.so* - dynamic linker/loader

DESCRIPTION
       The  programs ld.so and ld-linux.so* find and load the shared libraries
       needed by a program, prepare the program to run, and then run it.
. . .

......and......

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

NAME
       /sbin/ldconfig - configure dynamic linker run time bindings

SYNOPSIS
       /sbin/ldconfig  [  -nNvXV ] [ -f conf ] [ -C cache ] [ -r root ] direc-
       tory ...
       /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).  The cache is used by the run-time linker, ld.so or ld-
       linux.so.  ldconfig checks the header and filenames of the libraries it
       encounters  when  determining  which  versions  should have their links
       updated.
. . .

9
使用命令 find / -name 'libavdevice.so.*' 查找此库是否可用。
使用命令 sudo gedit /etc/ld.so.conf
添加以下行并保存:
include /usr/local/lib
include /usr

ldconfig


需要使用“include”这个词吗? - infoclogged
“include”是用于从文件中导入的保留字。在许多发行版中,/etc/ld.so.conf的默认内容为“include /etc/ld.so.conf.d/*.conf”,如果您查看/etc/ld.so.conf.d/目录,您会看到列出实际库路径的文本文件。对于列出库的文件,请使用“include”,而对于直接路径(如/usr/local/lib),则不需要使用“include”。 - Randoragon

7

据我所知,ld.so使用文件/etc/ld.so.conf列出要搜索共享对象的目录。您也可以使用环境变量LD_LIBRARY_PATH

Linux上的ELF头还可能包含RPATH条目。要检查RPATH条目,请运行

readelf -d ffmpeg | grep RPATH

你很可能无法从中获得任何结果。在编译时设置RPATH的方法如下:

gcc ... -wl, -rpath=MY_PATH

如果您想要执行目录,请使用\$ORIGIN

一些程序(例如chrpath)允许您编辑现有二进制文件的RPATH。

注意:任何设置为setuid的程序都不会使用LD_LIBRARY_PATH,因为这是一种安全风险。


5

对于这个老问题,另一种选择是使用LD_RUN_PATH。

export LD_RUN_PATH=/usr/local/lib

然后再次编译:

make
make install
ldconfig

比使用LD_LIBRARY_PATH更好的方法。原始参考来自@cweiske linuxmafia.com/faq/Admin/ld-lib-path.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接