linux中的ldd命令簡介


轉載自:http://blog.csdn.net/stpeace/article/details/47069215

 

 在linux中, 有些命令是大家通用的, 比如ls, rm, mv, cp等等, 這些我覺得沒有必要再細說了。 而有些命令, 只有開發人員才會用到的, 這類命令, 作為程序員的我們, 是有必要了解的, 有的甚至需要熟練使用。

 

        有的人總說, 這些命令不重要, 用的時候去查就行了, 這么多么扯淡的說法啊。 具體用法細節是可以可查, 但至少得知道有ldd這個東西吧。連ldd都不知道, 怎么知道ldd是干啥的呢?

 

       在本文中, 我們來介紹一下ldd命令, 盡管它非常簡單。  哦, 我突然想起來, 我有個朋友, 她的名字的是三個字, 首寫字母剛好是l, d, d, 有點意思。  在linux中, ldd是list, dynamic, dependencies的縮寫, 意思是, 列出動態庫依賴關系。  當然, 你也可以用ldd --help或者man ldd來看其用法。 下面, 我們也來看看:

 

       test.h的內容為:

 

[cpp]  view plain  copy
 
  1. void print();  

       test.c的內容為:

 

 

[cpp]  view plain  copy
 
  1. #include <stdio.h>  
  2. #include "test.h"  
  3.   
  4. void print()  
  5. {  
  6. printf("rainy days\n");  
  7. }  

       main.c的內容為:

 

 

[cpp]  view plain  copy
 
  1. #include "test.h"  
  2.   
  3. int main()  
  4. {  
  5.     print();  
  6.     return 0;  
  7. }  

 

      進行一系列的編譯, 並用ldd命令, 得到:

 

[plain]  view plain  copy
 
  1. [taoge@localhost learn_ldd]$ ls  
  2. main.c  test.c  test.h  
  3. [taoge@localhost learn_ldd]$ gcc -c main.c test.c  
  4. [taoge@localhost learn_ldd]$ gcc main.o test.o  
  5. [taoge@localhost learn_ldd]$ ls  
  6. a.out  main.c  main.o  test.c  test.h  test.o  
  7. [taoge@localhost learn_ldd]$ ./a.out   
  8. rainy days  
  9. [taoge@localhost learn_ldd]$   
  10. [taoge@localhost learn_ldd]$   
  11. [taoge@localhost learn_ldd]$   
  12. [taoge@localhost learn_ldd]$ ldd *  
  13. a.out:  
  14.     linux-gate.so.1 =>  (0x00ba1000)  
  15.     libc.so.6 => /lib/libc.so.6 (0x0087e000)  
  16.     /lib/ld-linux.so.2 (0x00858000)  
  17. main.c:  
  18. ldd: warning: you do not have execution permission for `./main.c'  
  19.     not a dynamic executable  
  20. main.o:  
  21. ldd: warning: you do not have execution permission for `./main.o'  
  22.     not a dynamic executable  
  23. test.c:  
  24. ldd: warning: you do not have execution permission for `./test.c'  
  25.     not a dynamic executable  
  26. test.h:  
  27. ldd: warning: you do not have execution permission for `./test.h'  
  28. lddlibc4: cannot read header from `./test.h'  
  29. test.o:  
  30. ldd: warning: you do not have execution permission for `./test.o'  
  31.     not a dynamic executable  
  32. [taoge@localhost learn_ldd]$   

      可以看到a.out依賴於libc.so.6這個庫, 而這個庫的路徑為/lib/libc.so.6

 

 

      我們繼續看使用靜態鏈接庫的情形:

 

[plain]  view plain  copy
 
  1. [taoge@localhost learn_ldd]$ ls  
  2. main.c  test.c  test.h  
  3. [taoge@localhost learn_ldd]$ gcc -c test.c  
  4. [taoge@localhost learn_ldd]$ ar rcs libtest.a test.o  
  5. [taoge@localhost learn_ldd]$ gcc main.c -L. -ltest  
  6. [taoge@localhost learn_ldd]$ ls  
  7. a.out  libtest.a  main.c  test.c  test.h  test.o  
  8. [taoge@localhost learn_ldd]$ ./a.out  
  9. rainy days  
  10. [taoge@localhost learn_ldd]$   
  11. [taoge@localhost learn_ldd]$   
  12. [taoge@localhost learn_ldd]$   
  13. [taoge@localhost learn_ldd]$ ldd *  
  14. a.out:  
  15.     linux-gate.so.1 =>  (0x00e7c000)  
  16.     libc.so.6 => /lib/libc.so.6 (0x0087e000)  
  17.     /lib/ld-linux.so.2 (0x00858000)  
  18. libtest.a:  
  19. ldd: warning: you do not have execution permission for `./libtest.a'  
  20.     not a dynamic executable  
  21. main.c:  
  22. ldd: warning: you do not have execution permission for `./main.c'  
  23.     not a dynamic executable  
  24. test.c:  
  25. ldd: warning: you do not have execution permission for `./test.c'  
  26.     not a dynamic executable  
  27. test.h:  
  28. ldd: warning: you do not have execution permission for `./test.h'  
  29. lddlibc4: cannot read header from `./test.h'  
  30. test.o:  
  31. ldd: warning: you do not have execution permission for `./test.o'  
  32.     not a dynamic executable  
  33. [taoge@localhost learn_ldd]$   

       這次用靜態庫, 結果還是差不多, 就沒什么好說的了。

 

 

       我們繼續看使用動態鏈接庫時的情形:

 

[plain]  view plain  copy
 
  1. [taoge@localhost learn_ldd]$ ls  
  2. main.c  test.c  test.h  
  3. [taoge@localhost learn_ldd]$ gcc -c test.c  
  4. [taoge@localhost learn_ldd]$ gcc -shared -fPIC -o libtest.so test.o  
  5. [taoge@localhost learn_ldd]$ gcc main.c -L. -ltest  
  6. [taoge@localhost learn_ldd]$ ls  
  7. a.out  libtest.so  main.c  test.c  test.h  test.o  
  8. [taoge@localhost learn_ldd]$ ./a.out   
  9. ./a.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory  
  10. [taoge@localhost learn_ldd]$   
  11. [taoge@localhost learn_ldd]$   
  12. [taoge@localhost learn_ldd]$   
  13. [taoge@localhost learn_ldd]$ ldd *  
  14. a.out:  
  15.     linux-gate.so.1 =>  (0x00f3d000)  
  16.     libtest.so => not found  
  17.     libc.so.6 => /lib/libc.so.6 (0x0087e000)  
  18.     /lib/ld-linux.so.2 (0x00858000)  
  19. libtest.so:  
  20.     linux-gate.so.1 =>  (0x0031d000)  
  21.     libc.so.6 => /lib/libc.so.6 (0x00110000)  
  22.     /lib/ld-linux.so.2 (0x00858000)  
  23. main.c:  
  24. ldd: warning: you do not have execution permission for `./main.c'  
  25.     not a dynamic executable  
  26. test.c:  
  27. ldd: warning: you do not have execution permission for `./test.c'  
  28.     not a dynamic executable  
  29. test.h:  
  30. ldd: warning: you do not have execution permission for `./test.h'  
  31. lddlibc4: cannot read header from `./test.h'  
  32. test.o:  
  33. ldd: warning: you do not have execution permission for `./test.o'  
  34.     not a dynamic executable  
  35. [taoge@localhost learn_ldd]$   
  36. [taoge@localhost learn_ldd]$ su root  
  37. Password:   
  38. [root@localhost learn_ldd]# cp libtest.so /usr/lib/  
  39. [root@localhost learn_ldd]# ./a.out   
  40. rainy days  
  41. [root@localhost learn_ldd]# exit  
  42. exit  
  43. [taoge@localhost learn_ldd]$ ./a.out   
  44. rainy days  
  45. [taoge@localhost learn_ldd]$  
  46. [taoge@localhost learn_ldd]$  
  47. [taoge@localhost learn_ldd]$  
  48. [taoge@localhost learn_ldd]$ ldd a.out   
  49.     linux-gate.so.1 =>  (0x00510000)  
  50.     libtest.so => /usr/libtest.so (0x00fe3000)  
  51.     libc.so.6 => /lib/libc.so.6 (0x0087e000)  
  52.     /lib/ld-linux.so.2 (0x00858000)  
  53. [taoge@localhost learn_ldd]$   

        首先, 我們可以看到, a.out依賴於libtest.so這個庫, 但是, 結果是not found, 找不到。 為什么呢? 因為在/usr/lib下面沒有libtest.so, 后來, 我把libtest.so拷貝過去(需要root權限), 就OK了。 另外, 我們也應該看到, libtest.so的依賴庫也是可以通過ldd命令找到的。

 

         當然, 如果不想自己寫程序, 但想試一下ldd命令, 那也可以, 直接如下:

 

[plain]  view plain  copy
 
  1. [taoge@localhost learn_ldd]$ ldd /bin/ls  
  2.     linux-gate.so.1 =>  (0x0052b000)  
  3.     libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)  
  4.     librt.so.1 => /lib/librt.so.1 (0x00a5c000)  
  5.     libcap.so.2 => /lib/libcap.so.2 (0x0489c000)  
  6.     libacl.so.1 => /lib/libacl.so.1 (0x048c9000)  
  7.     libc.so.6 => /lib/libc.so.6 (0x0087e000)  
  8.     libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)  
  9.     /lib/ld-linux.so.2 (0x00858000)  
  10.     libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)  
  11.     libattr.so.1 => /lib/libattr.so.1 (0x04d99000)  
  12. [taoge@localhost learn_ldd]$ ldd /bin/mv  
  13.     linux-gate.so.1 =>  (0x00944000)  
  14.     libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)  
  15.     librt.so.1 => /lib/librt.so.1 (0x00a5c000)  
  16.     libacl.so.1 => /lib/libacl.so.1 (0x048c9000)  
  17.     libattr.so.1 => /lib/libattr.so.1 (0x04d99000)  
  18.     libc.so.6 => /lib/libc.so.6 (0x00110000)  
  19.     libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)  
  20.     /lib/ld-linux.so.2 (0x00858000)  
  21.     libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)  
  22. [taoge@localhost learn_ldd]$   

 

 

        在實際linux開發與調試中, 要經常查看動態庫依賴關系, ldd用得還是比較多的, 特別是出現故障的時候。OK, ldd命令就簡單介紹到這里了, 雖然簡單, 但很實用, 故不可不知。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM