[Linux] 修改 shared library (DLL) 預設搜尋路徑

[Linux] 修改 shared library (DLL) 預設搜尋路徑

今天在跑一個程式時,發現它有一個關聯的 .so,因此跑不起來,

可是奇怪的是同樣的程式在一台 unit-testing 機器上卻可以正常執行…

 

查了一下,那台機器上在執行程式時,也沒有設定 LD_LIBRARY_PATH,

那為什麼還可以找到關聯的 .so 檔呢?

 

上網找了一下,原來除了 LD_LIBRARY_PATH 這個環境變數以外,還有其他的預設搜尋路徑,

主要就是 /etc/ld.so.conf 和 /etc/ld.so.conf.d/ 目錄下面各個檔案中的路徑~

舉例來說,我的 CentOS 7 的 /etc/ld.so.conf 的內容只有一行,

代表要包含 ld.so.conf.d 目錄下所有的 .conf 的內容:

include ld.so.conf.d/*.conf

 

而 /etc/ld.so.conf.d 目錄下有一堆的 .conf 檔,每個檔案裡面都可以放多個路徑,

例如 dynist-x86_64.conf 裡面包含了 /usr/lib64/dynist 這個目錄,

因此 .so 的搜尋路徑就會包含這個目錄:

testuser@localhost ~ $ egrep "" /etc/ld.so.conf.d/*
/etc/ld.so.conf.d/atlas-x86_64.conf:/usr/lib64/atlas
/etc/ld.so.conf.d/dyninst-x86_64.conf:/usr/lib64/dyninst
/etc/ld.so.conf.d/kernel-3.10.0-123.el7.x86_64.conf:# Placeholder file, no vDSO hwcap entries used in this kernel.
/etc/ld.so.conf.d/kernel-3.10.0-327.10.1.el7.x86_64.conf:# Placeholder file, no vDSO hwcap entries used in this kernel.
/etc/ld.so.conf.d/kernel-3.10.0-327.3.1.el7.x86_64.conf:# Placeholder file, no vDSO hwcap entries used in this kernel.
/etc/ld.so.conf.d/libiscsi-x86_64.conf:/usr/lib64/iscsi
/etc/ld.so.conf.d/mariadb-x86_64.conf:/usr/lib64/mysql
/etc/ld.so.conf.d/qt-x86_64.conf:/usr/lib64/qt-3.3/lib

 

如果有修改這些 .conf 檔的話,得重新執行 ldconfig 來重建 cache 才行:

sudo ldconfig

 

用 ldconfig -p 可以看到已經 cache 起來的 .so 檔案,例如: 

testuser@localhost ~ $ ldconfig -p
1262 libs found in cache `/etc/ld.so.cache'
p11-kit-trust.so (libc6,x86-64) => /lib64/p11-kit-trust.so
libzmq.so.3 (libc6,x86-64) => /lib64/libzmq.so.3
libzapojit-0.0.so.0 (libc6,x86-64) => /lib64/libzapojit-0.0.so.0
libz.so.1 (libc6,x86-64) => /lib64/libz.so.1
libz.so (libc6,x86-64) => /lib64/libz.so
libyelp.so.0 (libc6,x86-64) => /lib64/libyelp.so.0
libyaml-0.so.2 (libc6,x86-64) => /lib64/libyaml-0.so.2
libyajl.so.2 (libc6,x86-64) => /lib64/libyajl.so.2
libxtables.so.10 (libc6,x86-64) => /lib64/libxtables.so.10
libxslt.so.1 (libc6,x86-64) => /lib64/libxslt.so.1
libxshmfence.so.1 (libc6,x86-64) => /lib64/libxshmfence.so.1
libxml2.so.2 (libc6,x86-64) => /lib64/libxml2.so.2
libxmlrpc_util.so.3 (libc6,x86-64) => /lib64/libxmlrpc_util.so.3
......

 

參考資料:

What is the default location for dll on a linux system?

(本頁面已被瀏覽過 987 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料