今天在新開的Ubuntu虛擬機上安裝libevent,本來感覺沒啥需要特別注意的,因爲他自帶的README.md 文件就已經說清楚瞭如何安裝"安裝說明",無非就是簡單的幾步:
$ ./configure
$ make
$ make verify # (optional)
$ sudo make install
但是按照這個安裝完後,寫了一個測試程序去運行,發現他提示這類錯誤
error while loading shared libraries: libevent-2.xxxx ......
然後我注意到一個細節,就是我make install 成功後,libevent給出的說明信息:
Libraries have been installed in:
/usr/local/lib
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
這些信息說明了,我們在使用這個庫的時候,必須至少做以下四件事(翻譯的可能不太好):
-執行時將LIBDIR添加到“ LD_LIBRARY_PATH”環境變量
-鏈接時將LIBDIR添加到“ LD_RUN_PATH”環境變量
-使用'-Wl,-rpath -Wl,LIBDIR'鏈接標誌
-讓系統管理員將LIBDIR添加到'/etc/ld.so.conf'文件
因爲libevent已經提示了,他的安裝路徑"/usr/local/lib",查看該路徑,發現有這些文件:
drwxr-xr-x 4 root root 4096 3月 4 16:32 ./
drwxr-xr-x 14 root root 4096 1月 6 20:36 ../
lrwxrwxrwx 1 root root 21 3月 4 16:32 libevent-2.1.so.7 -> libevent-2.1.so.7.0.0*
-rwxr-xr-x 1 root root 1411256 3月 4 16:32 libevent-2.1.so.7.0.0*
......
而程序中要用的就是動態的so文件,因此我選擇做第四件事: "讓系統管理員將LIBDIR添加到'/etc/ld.so.conf'文件",因爲其他解決方法都要在每次運行程序或者執行鏈接的時候輸入額外的信息,但是第四件事做好了,以後都不需要做額外工作了,因此:
轉到root賬戶:su -
vim /etc/ld.so.conf
在文件最後一行增加so路徑信息:/usr/local/lib/libevent-2.1.so.7
保存然後關閉文件
執行ldconfig
再執行libevent的測試程序的時候就沒啥問題了.