編譯 memcached 時報找不到 libevent,於是下載 libevent 並 ./configure && make && make install,之後 memcached 也順利裝好了,預設位於 /usr/local/bin/。誰知執行時卻出問題:
[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
libevent 明明裝了,問題應該出在 memcached 找不到它。先確認 libevent 的實際位置:
[root@localhost bin]# whereis libevent libevent: /usr/local/lib/libevent.la /usr/local/lib/libevent.a /usr/local/lib/libevent.so
接著看 memcached 的相依狀態:
[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-2.0.so.5 => not found。用 LD_DEBUG 觀察載入器到底找了哪些路徑:
[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/libevent-2.0.so.5 10071: trying file=/usr/lib/libevent-2.0.so.5
原因很清楚了:系統搜尋路徑裡沒有 /usr/local/lib,而 libevent 恰好裝在那裡。以下三種解法都可以。
連到搜尋路徑中的任一目錄即可:
[root@localhost bin]# ln -s /usr/local/lib/libevent.so /usr/lib/libevent-2.0.so.5
驗證:
[root@localhost bin]# memcached -help memcached 1.4.24 -p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -m <num> max memory to use for items in megabytes (default: 64 MB) -c <num> max simultaneous connections (default: 1024) -d run as a daemon ... 其餘參數略 ...
不再報錯,代表問題已解決。
先看 configure 支援哪些選項:
[root@localhost libevent]# ./configure --help Installation directories: --prefix=PREFIX install architecture-independent files in PREFIX [/usr/local] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] Some influential environment variables: CC C compiler command CFLAGS C compiler flags LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a nonstandard directory LIBS libraries to pass to the linker, e.g. -l<library>
所以可以直接在 configure 時指定:
[root@localhost libevent]# ./configure --libdir=/usr/lib
把 /usr/local/lib 加進環境變數。這種做法一般不推薦全域使用,但有變通方式,請見下方總結第 3 點。
1. 共享函式庫相關錯誤怎麼查
① LD_DEBUG——glibc 載入器提供的除錯環境變數,可觀察整個載入過程:
[root@localhost libevent]# LD_DEBUG=help ls Valid options for the LD_DEBUG environment variable are: libs display library search paths reloc display relocation processing files display progress for input file symbols display symbol table processing bindings display information about symbol binding versions display version dependencies all all previous options combined statistics display relocation statistics unused determined unused DSOs help display this help message and exit To direct the debugging output into a file instead of standard output a filename can be specified using the LD_DEBUG_OUTPUT environment variable.
② ldd——列出相依函式庫,最快速的一眼判斷。
③ LD_LIBRARY_PATH、/etc/ld.so.conf.d、ldconfig——載入器會去 /etc/ld.so.conf.d 底下讀取額外的共享函式庫目錄設定,因此最正規的做法是為 /usr/local/lib 專門建一個設定檔:
[root@localhost ld.so.conf.d]# pwd /etc/ld.so.conf.d [root@localhost ld.so.conf.d]# echo "/usr/local/lib" > usr_local_lib.conf [root@localhost ld.so.conf.d]# cat usr_local_lib.conf /usr/local/lib [root@localhost ld.so.conf.d]# ldconfig
這樣 /usr/local/lib 下的共享函式庫就會被找到。關鍵:改完載入器相關設定後一定要跑 ldconfig 更新快取,否則依然找不到。這個做法比到處補軟連結乾淨,也更便於後續維護。
2. 怎麼找檔案與函式庫
· whereis:定位二進位檔、原始碼與 man 手冊
· updatedb:更新 locate 用的資料庫
· locate:依檔名搜尋
· find:在目錄階層中搜尋
find / -name libevent.* find /usr/local -name libevent.* find / -name libevent.so*
find 的第一個參數是搜尋路徑,第二個是檔名表達式(可用正規表達式)。它還有一個常見用法是對結果執行指令:
find . -name \*.lo -o -name \*.o | xargs rm -f
3. 養成習慣:configure 之前一律先看 ./configure --help,多數「裝了卻找不到」的問題,都是因為沒注意到 --libdir 或 --prefix 這類選項。