本篇 ShengYu 介紹用 Linux ldd 指令來印出執行檔所相依的共享函式庫 shared library,也可以用 readelf 達成同樣的功能。
我們先隨便寫個 c++ 程式,並且假設為 main.cpp,接著用 g++ 來編譯,預設會連結到 libstdc++,
之後我們使用 Linux ldd 指令來印出 a.out 執行檔需要哪些 shared library,1
2
3
4
5
6
7
8$ g++ main.cpp -o a.out
$ ldd a.out
linux-vdso.so.1 => (0x00007ffcf11fb000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f660acdb000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f660aac5000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f660a6fb000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f660a3f1000)
/lib64/ld-linux-x86-64.so.2 (0x000055a069d1f000)
我們這次換個例子,用同樣的程式,這次改連到別的 shared library 試試,
那我們換成用 clang++ 編譯,並且連結到 libc++,用 ldd 指令來印出剛剛編譯出來的 a.out 執行檔需要哪些 shared library,1
2
3
4
5
6
7
8
9
10$ clang++ -stdlib=libc++ main.cpp -o a.out
$ ldd a.out
linux-vdso.so.1 => (0x00007ffd0e7ea000)
libc++.so.1 => /usr/lib/x86_64-linux-gnu/libc++.so.1 (0x00007fa7fb66b000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa7fb362000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa7fb14c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa7fad81000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa7fab64000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fa7fa95c000)
/lib64/ld-linux-x86-64.so.2 (0x000055f1e5a38000)
查看某執行檔的依賴函式庫
這邊示範查看 gdb 的依賴函式庫,1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20$ ldd `which gdb` # 或者 ldd /usr/bin/gdb
linux-vdso.so.1 => (0x00007ffc41f25000)
libreadline.so.6 => /lib/x86_64-linux-gnu/libreadline.so.6 (0x00007efeca068000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007efec9e4e000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007efec9c4a000)
libncurses.so.5 => /lib/x86_64-linux-gnu/libncurses.so.5 (0x00007efec9a27000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007efec97fe000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007efec94f5000)
libpython3.5m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.5m.so.1.0 (0x00007efec8e6d000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007efec8c50000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007efec8a27000)
liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007efec8804000)
libbabeltrace.so.1 => /usr/lib/x86_64-linux-gnu/libbabeltrace.so.1 (0x00007efec85f7000)
libbabeltrace-ctf.so.1 => /usr/lib/x86_64-linux-gnu/libbabeltrace-ctf.so.1 (0x00007efec83bd000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007efec7ff2000)
/lib64/ld-linux-x86-64.so.2 (0x000055d685a4c000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007efec7def000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007efec7add000)
libuuid.so.1 => /lib/x86_64-linux-gnu/libuuid.so.1 (0x00007efec78d8000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007efec7668000)
查看某函式庫的依賴函式庫
這邊示範查看 libpthread.so 的依賴函式庫,1
2
3
4$ ldd `gcc -print-file-name=libpthread.so.0` # 或者 ldd /lib/x86_64-linux-gnu/libpthread.so.0
linux-vdso.so.1 => (0x00007ffcd2929000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd55bc64000)
/lib64/ld-linux-x86-64.so.2 (0x0000555d65b68000)
以上就是 Linux ldd 查看執行檔執行時需要哪些 library的介紹,
如果你覺得我的文章寫得不錯、對你有幫助的話記得 Facebook 按讚支持一下!
其它參考
How to show all shared libraries used by executables in Linux? - Stack Overflow
https://stackoverflow.com/questions/50159/how-to-show-all-shared-libraries-used-by-executables-in-linux
其它相關文章推薦
Linux 常用指令教學懶人包
readelf 用法與範例
nm 用法與範例
objdump 用法與範例
clang++ 如何使用 libc++