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的内容为:
- void print();
test.c的内容为:
- #include <stdio.h>
- #include "test.h"
- void print()
- {
- printf("rainy days\n");
- }
main.c的内容为:
- #include "test.h"
- int main()
- {
- print();
- return 0;
- }
进行一系列的编译, 并用ldd命令, 得到:
- [taoge@localhost learn_ldd]$ ls
- main.c test.c test.h
- [taoge@localhost learn_ldd]$ gcc -c main.c test.c
- [taoge@localhost learn_ldd]$ gcc main.o test.o
- [taoge@localhost learn_ldd]$ ls
- a.out main.c main.o test.c test.h test.o
- [taoge@localhost learn_ldd]$ ./a.out
- rainy days
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$ ldd *
- a.out:
- linux-gate.so.1 => (0x00ba1000)
- libc.so.6 => /lib/libc.so.6 (0x0087e000)
- /lib/ld-linux.so.2 (0x00858000)
- main.c:
- ldd: warning: you do not have execution permission for `./main.c'
- not a dynamic executable
- main.o:
- ldd: warning: you do not have execution permission for `./main.o'
- not a dynamic executable
- test.c:
- ldd: warning: you do not have execution permission for `./test.c'
- not a dynamic executable
- test.h:
- ldd: warning: you do not have execution permission for `./test.h'
- lddlibc4: cannot read header from `./test.h'
- test.o:
- ldd: warning: you do not have execution permission for `./test.o'
- not a dynamic executable
- [taoge@localhost learn_ldd]$
可以看到a.out依赖于libc.so.6这个库, 而这个库的路径为/lib/libc.so.6
我们继续看使用静态链接库的情形:
- [taoge@localhost learn_ldd]$ ls
- main.c test.c test.h
- [taoge@localhost learn_ldd]$ gcc -c test.c
- [taoge@localhost learn_ldd]$ ar rcs libtest.a test.o
- [taoge@localhost learn_ldd]$ gcc main.c -L. -ltest
- [taoge@localhost learn_ldd]$ ls
- a.out libtest.a main.c test.c test.h test.o
- [taoge@localhost learn_ldd]$ ./a.out
- rainy days
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$ ldd *
- a.out:
- linux-gate.so.1 => (0x00e7c000)
- libc.so.6 => /lib/libc.so.6 (0x0087e000)
- /lib/ld-linux.so.2 (0x00858000)
- libtest.a:
- ldd: warning: you do not have execution permission for `./libtest.a'
- not a dynamic executable
- main.c:
- ldd: warning: you do not have execution permission for `./main.c'
- not a dynamic executable
- test.c:
- ldd: warning: you do not have execution permission for `./test.c'
- not a dynamic executable
- test.h:
- ldd: warning: you do not have execution permission for `./test.h'
- lddlibc4: cannot read header from `./test.h'
- test.o:
- ldd: warning: you do not have execution permission for `./test.o'
- not a dynamic executable
- [taoge@localhost learn_ldd]$
这次用静态库, 结果还是差不多, 就没什么好说的了。
我们继续看使用动态链接库时的情形:
- [taoge@localhost learn_ldd]$ ls
- main.c test.c test.h
- [taoge@localhost learn_ldd]$ gcc -c test.c
- [taoge@localhost learn_ldd]$ gcc -shared -fPIC -o libtest.so test.o
- [taoge@localhost learn_ldd]$ gcc main.c -L. -ltest
- [taoge@localhost learn_ldd]$ ls
- a.out libtest.so main.c test.c test.h test.o
- [taoge@localhost learn_ldd]$ ./a.out
- ./a.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$ ldd *
- a.out:
- linux-gate.so.1 => (0x00f3d000)
- libtest.so => not found
- libc.so.6 => /lib/libc.so.6 (0x0087e000)
- /lib/ld-linux.so.2 (0x00858000)
- libtest.so:
- linux-gate.so.1 => (0x0031d000)
- libc.so.6 => /lib/libc.so.6 (0x00110000)
- /lib/ld-linux.so.2 (0x00858000)
- main.c:
- ldd: warning: you do not have execution permission for `./main.c'
- not a dynamic executable
- test.c:
- ldd: warning: you do not have execution permission for `./test.c'
- not a dynamic executable
- test.h:
- ldd: warning: you do not have execution permission for `./test.h'
- lddlibc4: cannot read header from `./test.h'
- test.o:
- ldd: warning: you do not have execution permission for `./test.o'
- not a dynamic executable
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$ su root
- Password:
- [root@localhost learn_ldd]# cp libtest.so /usr/lib/
- [root@localhost learn_ldd]# ./a.out
- rainy days
- [root@localhost learn_ldd]# exit
- exit
- [taoge@localhost learn_ldd]$ ./a.out
- rainy days
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$
- [taoge@localhost learn_ldd]$ ldd a.out
- linux-gate.so.1 => (0x00510000)
- libtest.so => /usr/libtest.so (0x00fe3000)
- libc.so.6 => /lib/libc.so.6 (0x0087e000)
- /lib/ld-linux.so.2 (0x00858000)
- [taoge@localhost learn_ldd]$
首先, 我们可以看到, a.out依赖于libtest.so这个库, 但是, 结果是not found, 找不到。 为什么呢? 因为在/usr/lib下面没有libtest.so, 后来, 我把libtest.so拷贝过去(需要root权限), 就OK了。 另外, 我们也应该看到, libtest.so的依赖库也是可以通过ldd命令找到的。
当然, 如果不想自己写程序, 但想试一下ldd命令, 那也可以, 直接如下:
- [taoge@localhost learn_ldd]$ ldd /bin/ls
- linux-gate.so.1 => (0x0052b000)
- libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)
- librt.so.1 => /lib/librt.so.1 (0x00a5c000)
- libcap.so.2 => /lib/libcap.so.2 (0x0489c000)
- libacl.so.1 => /lib/libacl.so.1 (0x048c9000)
- libc.so.6 => /lib/libc.so.6 (0x0087e000)
- libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)
- /lib/ld-linux.so.2 (0x00858000)
- libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)
- libattr.so.1 => /lib/libattr.so.1 (0x04d99000)
- [taoge@localhost learn_ldd]$ ldd /bin/mv
- linux-gate.so.1 => (0x00944000)
- libselinux.so.1 => /lib/libselinux.so.1 (0x00b52000)
- librt.so.1 => /lib/librt.so.1 (0x00a5c000)
- libacl.so.1 => /lib/libacl.so.1 (0x048c9000)
- libattr.so.1 => /lib/libattr.so.1 (0x04d99000)
- libc.so.6 => /lib/libc.so.6 (0x00110000)
- libdl.so.2 => /lib/libdl.so.2 (0x00a0c000)
- /lib/ld-linux.so.2 (0x00858000)
- libpthread.so.0 => /lib/libpthread.so.0 (0x00a13000)
- [taoge@localhost learn_ldd]$
在实际linux开发与调试中, 要经常查看动态库依赖关系, ldd用得还是比较多的, 特别是出现故障的时候。OK, ldd命令就简单介绍到这里了, 虽然简单, 但很实用, 故不可不知。
linux中的ldd命令简介的更多相关文章
- linux中的strings命令简介2
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 之前我们聊过linux strings的用法和用 ...
- linux中的strings命令简介
摘自:http://blog.csdn.net/stpeace/article/details/46641069 linux中的strings命令简介 在linux下搞软件开发的朋友, 几乎没有不知道 ...
- linux中的strip命令简介------给文件脱衣服
1.去掉-g,等于程序做了--strip-debug2.strip程序,等于程序做了--strip-debug和--strip-symbol 作为一名Linux开发人员, 如果没有听说过strip命令 ...
- linux中的strip命令简介------给文件脱衣服【转】
转自:http://blog.csdn.net/stpeace/article/details/47090255 版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则 ...
- linux中的nm命令简介
转:http://blog.csdn.net/stpeace/article/details/47089585 一般来说, 搞linux开发的人, 才会用到nm命令, 非开发的人, 应该用不到. 虽然 ...
- linux中的strip命令简介
转载:https://blog.csdn.net/qq_37858386/article/details/78559490 strip:去除,剥去 一.下面是man strip获得到的信息,简 ...
- Python学习之旅:使用Python实现Linux中的ls命令
一.写在前面 前几天在微信上看到这样一篇文章,链接为:https://mp.weixin.qq.com/s/rl6Sgv3uk_IpoFAx6cWa8w,在这篇文章中,有这样一段话,吸引了我的注意: ...
- Linux中的历史命令
Linux中的历史命令一般保存在用户 /root/.bash_history history 选项 历史命令保存文件夹 选项 -c:清空历史命令 -w :把缓存中的历史命令写入历 ...
- 关于XShell的常见使用和设置以及Linux中的常见命令.
本文部分转自:http://sundful.iteye.com/blog/704079 和 http://www.vckai.com/p/5 有时候在XShell中操作的一些命令傻傻的分不清这个命令到 ...
随机推荐
- 使用 Laravel-Excel 进行 CSV/EXCEL 文件读写
https://blog.csdn.net/yiluohan0307/article/details/80229978 http://www.ptbird.cn/laravel-excel-csv.h ...
- python-字符编码数据类型转换
1 - 编码格式转换 1.1 编码格式介绍 字符集 介绍 ASCII ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符 ANSI ANSI是一种字符代码,为使计算 ...
- pip安装软件包报Could not fetch URL
报这个错误的原因是python.org已经不支持TLSv1.0和TLSv1.1了.更新pip可以解决这个问题,但是你不能用命令 pip install --upgrade pip 做更新,因为TLS证 ...
- clone() 方法
<html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...
- [转]SpringBoot整合Swagger2以及生产环境的安全问题处理
1.创建springboot项目 https://www.cnblogs.com/i-tao/p/8878562.html 这里我们使用多环境配置: application-dev.yml(开发环境) ...
- pytorch中如何处理RNN输入变长序列padding
一.为什么RNN需要处理变长输入 假设我们有情感分析的例子,对每句话进行一个感情级别的分类,主体流程大概是下图所示: 思路比较简单,但是当我们进行batch个训练数据一起计算的时候,我们会遇到多个训练 ...
- 【u128】又一个数字游戏
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明拿出了一个素数集合,{2, 3, 5, 7, 11, 13, -, 127, -},他发现,从小到 ...
- Ubuntu Kylin 14.04安装
早听说Ubuntu Kylin对中国本地做了很多定制的工作,想搜狗输入法.WPS,还有中国日历等.昨天没事就下载了一个Kylin试用了下,使用的方法还是使用EasyBCD软件做了个硬盘安装启动,关于E ...
- async和await的执行顺序问题
说明 : 要了解执行顺序,所需要的知识是了解浏览器js运行机制,以及微任务和宏任务的先后顺序.如果你明白了宏任务.微任务,请往下看: async function async1 () { consol ...
- Server,Servlet,ServletConfig,ServletContext,Session,Request,Response
Server流程 解析URL->找到应用->找到Servlet->实例化Servlet->调用init->调用service->返回响应->调用destroy ...