最简单的解决方法:
第一步、修改 sudo vim /etc/ld.so.conf 添加动态库的指定路径
第二步、sudo ldconfig 生效
如果找不到制定的 执行程序,例如ffmpeg ,直接添加 Path 即可
临时的修改方法:下次失效
设置动态库和 执行程序的搜索路径: 一般将程序安装在 /usr/local/特性的目录下
利用系统的环境变量。
对于头文件的搜索路径:
C_INCLUDE_PATH=<your include path>;
export C_INCLUDE_PATH
对于库文件的搜索路径:
LIBRARY_PATH=<your lib path>;
export LIBRARY_PATH
对于链接程序ld使用的库文件搜索路径:
LD_LIBRARY_PATH=<your ldlib path>;
export LD_LIBRARY_PATH
最后是执行 ldconfig ,生效
最愚蠢的解决方法,但是里面可以查看到库,以及搜索路径
第一:ldd 应用程序 查看依赖库,是不是有not found的库
执行命令 ./memcache -help 报错:
[root@localhost bin]# ./memcached ./memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
[root@localhost bin]# ldd /usr/local/bin/memcached linux-gate.so.1 => (0x00c8d000) libevent-2.0.so.5 => not found librt.so.1 => /lib/librt.so.1 (0x00720000) libpthread.so.0 => /lib/libpthread.so.0 (0x006fc000) libc.so.6 => /lib/libc.so.6 (0x00564000) /lib/ld-linux.so.2 (0x0053e000)
第二:查找是不是真正的没有该依赖库
查找 libevent 安装的路径:
[root@localhost bin]# whereis libevent
libevent: /usr/local/lib/libevent.la /usr/local/lib/libevent.a /usr/local/lib/libevent.so
我们看到被安装到了:/usr/local/lib/libevent.so
第三:
查看 libevent 如何寻找依赖:
[root@localhost bin]# LD_DEBUG=libs ./memcached -v 10071: find library=libevent-2.0.so.5 [0]; searching 10071: search cache=/etc/ld.so.cache 10071: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path) 10071: trying file=/lib/tls/i686/sse2/libevent-2.0.so.5 10071: trying file=/lib/tls/i686/libevent-2.0.so.5 10071: trying file=/lib/tls/sse2/libevent-2.0.so.5 10071: trying file=/lib/tls/libevent-2.0.so.5 10071: trying file=/lib/i686/sse2/libevent-2.0.so.5 10071: trying file=/lib/i686/libevent-2.0.so.5 10071: trying file=/lib/sse2/libevent-2.0.so.5 10071: trying file=/lib/libevent-2.0.so.5 10071: trying file=/usr/lib/tls/i686/sse2/libevent-2.0.so.5 10071: trying file=/usr/lib/tls/i686/libevent-2.0.so.5 10071: trying file=/usr/lib/tls/sse2/libevent-2.0.so.5 10071: trying file=/usr/lib/tls/libevent-2.0.so.5 10071: trying file=/usr/lib/i686/sse2/libevent-2.0.so.5 10071: trying file=/usr/lib/i686/libevent-2.0.so.5 10071: trying file=/usr/lib/sse2/libevent-2.0.so.5 10071: trying file=/usr/lib/libevent-2.0.so.5
我们看到:
search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:
/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:
/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path)
系统寻找 lib 的路径,中没有包含:/usr/local/lib/
所以找到了原因,解决方法就很简单了。也有多种方法。
1) 做一个软连接到上面的随便一个 search path 中的目录都是可以的:
[root@localhost bin]# ln -s /usr/local/lib/libevent.so /usr/lib/libevent-2.0.so.5