場景:
編譯程式時使用了如下參數指定了連結庫的位置,但是執行時提示無法加載共享庫。
#gcc test.c -L /usr/local/rrdtool-1.2.30/lib -lrrd
現象:
error while loading libraries:librrd.so.2:cannot open shared object file:No such file or directory
解決方法:
使用ldd指令 檢視編譯生成的可執行檔案發現:
#ldd a.out
指令輸出結果:
librrd.so.2 => not found
修改/etc/ld.so.conf檔案添加 目錄——/usr/local/rrdtool-1.2.30/lib
執行如下指令:
#ldconfig
再次執行如下指令:
本身輸出:
librrd.so.2 => /usr/local/rrdtool-1.2.30/lib/librrd.so.2
原理:
編譯時确實是通過了,因為你編譯時指定了編譯時需要的有關該共享庫的資訊。
但是沒有指定運作時(run time)所需要的資訊;man一把ldconfig指令就知道原因了。
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(/etc/ld.so.cache
) is used by the run-time linker, ld.so or ld-linux.so.
需要的動态來接庫的目錄路徑,必須在/etc/ld.so.cache 檔案中,而該檔案是通過ld.so.conf指定的目錄,以及信任目錄(/lib and /usr/lib),使用ldconfig指令生成的。ldd指令也檢視可執行檔案所依賴的動态庫資訊時,也使用了/etc/ld.so.cache檔案,是以在沒有将所需要的動态庫目錄添加到ld.so.conf中且執行ldconfig時,會提示該庫"not found"