转载自: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的内容为:

  1. void print();

test.c的内容为:

  1. #include <stdio.h>
  2. #include "test.h"
  3. void print()
  4. {
  5. printf("rainy days\n");
  6. }

main.c的内容为:

  1. #include "test.h"
  2. int main()
  3. {
  4. print();
  5. return 0;
  6. }

进行一系列的编译, 并用ldd命令, 得到:

  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

我们继续看使用静态链接库的情形:

  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]$

这次用静态库, 结果还是差不多, 就没什么好说的了。

我们继续看使用动态链接库时的情形:

  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命令, 那也可以, 直接如下:

  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命令就简单介绍到这里了, 虽然简单, 但很实用, 故不可不知。

linux中的ldd命令简介的更多相关文章

  1. linux中的strings命令简介2

    摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用 ...

  2. linux中的strings命令简介

    摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...

  3. linux中的strip命令简介------给文件脱衣服

    1.去掉-g,等于程序做了--strip-debug2.strip程序,等于程序做了--strip-debug和--strip-symbol 作为一名Linux开发人员, 如果没有听说过strip命令 ...

  4. linux中的strip命令简介------给文件脱衣服【转】

    转自:http://blog.csdn.net/stpeace/article/details/47090255 版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则 ...

  5. linux中的nm命令简介

    转:http://blog.csdn.net/stpeace/article/details/47089585 一般来说, 搞linux开发的人, 才会用到nm命令, 非开发的人, 应该用不到. 虽然 ...

  6. linux中的strip命令简介

    转载:https://blog.csdn.net/qq_37858386/article/details/78559490 strip:去除,剥去     一.下面是man strip获得到的信息,简 ...

  7. Python学习之旅:使用Python实现Linux中的ls命令

    一.写在前面 前几天在微信上看到这样一篇文章,链接为:https://mp.weixin.qq.com/s/rl6Sgv3uk_IpoFAx6cWa8w,在这篇文章中,有这样一段话,吸引了我的注意: ...

  8. Linux中的历史命令

    Linux中的历史命令一般保存在用户    /root/.bash_history history 选项 历史命令保存文件夹 选项     -c:清空历史命令     -w :把缓存中的历史命令写入历 ...

  9. 关于XShell的常见使用和设置以及Linux中的常见命令.

    本文部分转自:http://sundful.iteye.com/blog/704079 和 http://www.vckai.com/p/5 有时候在XShell中操作的一些命令傻傻的分不清这个命令到 ...

随机推荐

  1. E. Remainder Problem 分块

    两个操作 1对x位置的a[x]+y 2对所有i=y(mod x)求a[i]的和 我们肯定不能n^2 跑,稳超时,但是我们可以这样分块考虑. 为什么n^2不行?因为在x比较小的时候,这个求和操作次数太多 ...

  2. hdu 2473 Junk-Mail Filter (暴力并查集)

    Problem - 2473 为什么标题写的是暴力并查集?因为我的解法跟网上的有所不同,方法暴力很多. 先解释题意,这是一个模拟处理垃圾邮件的问题.垃圾邮件要根据它们的性质进行分类.对于10w个邮件, ...

  3. Eclipse(Maven) web项目更改项目名称

    1. 右键工程:Refactor->Rename,更改项目名称: 2. 修改项目目录下:.project文件 <?xml version="1.0" encoding= ...

  4. name和code的相关设置

    cdm中同时显示name和code tools->或右键Displace preferences->Entity->Advanced->Attributes->右侧Lis ...

  5. oracle避免在索引列上使用IS NULL和IS NOT NULL

    避免在索引中使用任何可以为空的列,ORACLE将无法使用该索引 .对于单列索引,如果列包含空值,索引中将不存在此记录. 对于复合索引,如果每个列都为空,索引中同样不存在此记录. 如果至少有一个列不为空 ...

  6. 永久设置anaconda的环境变量

    安装anaconda后都显示install seccessful,可是输入anaconda 终端却显示“未找到命令” 原因是没有添加环境变量,按照如下方式将环境变量添加的安装路径下: emport P ...

  7. Android 自定义ProgressDialog

    Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框等待网络数据. 不过,既然是为了提高用户体验,我们肯定希望该 ...

  8. uni-app学习记录02-属性绑定.for循环

    <template> <view class="content"> <text> 我是首页 </text> <!-- 输出纯字 ...

  9. H3C NAT Server配置举例

  10. window 系统下修改`CMD`的编码格式的方法,`CHCP` 的 使用

    CHCP的使用 CHCP是一个计算机指令,能够显示或设置活动代码页编号. 一般上是在命令提示框中使用,用来查询和修改命令提示框的编码格式 具体使用方法 查看活动代码页编号 方式1: >>& ...