測試環境為 CentOS 6 x86_64 虛擬機.
Linux 上大部分的程式是採用 Dynamical Link 方式來編譯,我們可以透過 ldconfig 或是 ldd 來觀察 dynamically linked (uses shared libs) 的狀態.
ldconfig – configure dynamic linker run-time bindings
哪一些函數庫需要再開機的時就預先載入到記憶體,這定義在 /etc/ld.so.conf 檔案裡面.
[root@localhost ~]# cat /etc/ld.so.conf include ld.so.conf.d/*.conf /usr/local/lib
使用者通常可以自行定義在 /etc/ld.so.conf.d/*.conf ,像是 vmware 就定義了 vmware-tools-libraries.conf .
[root@localhost ~]# cat /etc/ld.so.conf.d/vmware-tools-libraries.conf /usr/lib/vmware-tools/lib32/libvmGuestLib.so /usr/lib/vmware-tools/lib64/libvmGuestLib.so /usr/lib/vmware-tools/lib32/libvmGuestLibJava.so /usr/lib/vmware-tools/lib64/libvmGuestLibJava.so /usr/lib/vmware-tools/lib32/libDeployPkg.so /usr/lib/vmware-tools/lib64/libDeployPkg.so
透過 ldconfig 指令來看目前哪一些函數庫已經被載入
[root@localhost ~]# ldconfig -p | grep -i vmguest libvmGuestLibJava.so (libc6,x86-64) => /usr/lib/vmware-tools/lib64/libvmGuestLibJava.so/libvmGuestLibJava.so libvmGuestLibJava.so (libc6) => /usr/lib/vmware-tools/lib32/libvmGuestLibJava.so/libvmGuestLibJava.so libvmGuestLib.so (libc6,x86-64) => /usr/lib/vmware-tools/lib64/libvmGuestLib.so/libvmGuestLib.so libvmGuestLib.so (libc6) => /usr/lib/vmware-tools/lib32/libvmGuestLib.so/libvmGuestLib.so
ldconfig 參數:
-p : Print the lists of directories and candidate libraries stored in the current cache.
ldd – print shared library
ldd 是個 script file 如果想知道該程式使用了哪一些 Libary 就可以使用 ldd 來觀看.
首先哪些程式使用了動態連結 dynamically linked (使用共享函數庫 shared libs) ,可以先透過 file 指令來觀看.
[root@localhost ~]# file /bin/ls /bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped
指令 ls 的確有使用 dynamically linked (uses shared libs) ,這時候可以透過 ldd 來檢視他使用了哪一些動態連結函數庫 (so 檔).
[root@localhost ~]# ldd /bin/ls linux-vdso.so.1 => (0x00007ffe62d78000) libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003bf4000000) librt.so.1 => /lib64/librt.so.1 (0x0000003bf3000000) libcap.so.2 => /lib64/libcap.so.2 (0x0000003bf7800000) libacl.so.1 => /lib64/libacl.so.1 (0x0000003bfd800000) libc.so.6 => /lib64/libc.so.6 (0x0000003bf2800000) libdl.so.2 => /lib64/libdl.so.2 (0x0000003bf2400000) /lib64/ld-linux-x86-64.so.2 (0x0000003bf2000000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003bf2c00000) libattr.so.1 => /lib64/libattr.so.1 (0x0000003c01000000)
LD_LIBRARY_PATH
Library 的路徑可以透過定義 LD_LIBRARY_PATH 變數來指定.
可以直接寫在使用者的 .bashrc 檔案裏.
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/ export LD_LIBRARY_PATH
沒有解決問題,試試搜尋本站其他內容
One thought on “Linux command – ldconfig & ldd”