我有一個依賴于 libB.so 的 libA.so,即使它位于同一目錄中也很難找到它。
ldd libA.so
linux-vdso.so.1 (0x00007fff50bdb000)
libB.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4aeb902000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4aebadb000)
我想知道是否有辦法讓 libA.so 始終在同一目錄中查找 libB.so ,因為我的應用程式就是這種情況?我知道更新LD_LIBRARY_PATH也是一種選擇,但想減少所需的作業量。
uj5u.com熱心網友回復:
.dynamicELF 檔案的部分( .soLinux 上的庫使用 ELF 格式)包含幫助庫找到其依賴項的資訊。.dynamictype 的條目DT_NEEDED包含動態聯結器要查找的其他.so檔案的名稱,但它們不包含有關在何處找到這些檔案的任何資訊。為此,正如您所提到的,您可以使用LD_LIBRARY_PATH,但 ELF 格式還提供了一種在檔案本身中指定它的方法。
具有型別的.dynamic條目DT_RUNPATH為動態聯結器提供了動態聯結器應在其中查找DT_NEEDED檔案的目錄的路徑。DT_RUNPATH允許一個特殊變數 ,$ORIGIN它參考檔案的當前目錄。這允許您使用相對路徑,而無需用戶從特定作業目錄呼叫可執行檔案。
您使用-rpath聯結器標志來指定一個DT_RUNPATH條目。但是,為了傳遞文字 string $ORIGIN,您必須將其包裹在單引號中,以防止您的 shell 將其解釋為環境變數。
假設您正在使用gcc,您應該使用將此引數添加到鏈接步驟:
-Wl,-rpath,'$ORIGIN'
uj5u.com熱心網友回復:
來自“男人 8 ld.so”:
If a shared object dependency does not contain a slash, then it
is searched for in the following order:
o Using the directories specified in the DT_RPATH dynamic
section attribute of the binary if present and DT_RUNPATH
attribute does not exist. Use of DT_RPATH is deprecated.
o Using the environment variable LD_LIBRARY_PATH, unless the
executable is being run in secure-execution mode (see below),
in which case this variable is ignored.
o Using the directories specified in the DT_RUNPATH dynamic
section attribute of the binary if present. Such directories
are searched only to find those objects required by DT_NEEDED
(direct dependencies) entries and do not apply to those
objects' children, which must themselves have their own
DT_RUNPATH entries. This is unlike DT_RPATH, which is applied
to searches for all children in the dependency tree.
o From the cache file /etc/ld.so.cache, which contains a
compiled list of candidate shared objects previously found in
the augmented library path. If, however, the binary was
linked with the -z nodeflib linker option, shared objects in
the default paths are skipped. Shared objects installed in
hardware capability directories (see below) are preferred to
other shared objects.
o In the default path /lib, and then /usr/lib. (On some 64-bit
architectures, the default paths for 64-bit shared objects are
/lib64, and then /usr/lib64.) If the binary was linked with
the -z nodeflib linker option, this step is skipped.
這里的關鍵是使用 DT_RUNPATH,它可以嵌入到您正在創建的二進制檔案中。您可以根據需要鏈接它,使其指向同一個目錄。
請參閱這篇關于如何執行此操作的帖子:https ://stackoverflow.com/a/67131878
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442237.html
上一篇:如何從ansibleplaybook中的檔案中讀取所有行
下一篇:jq帶單引號bash腳本
