摘自:http://blog.csdn.net/stpeace/article/details/46641069

linux中的strings命令简介

在linux下搞软件开发的朋友, 几乎没有不知道strings命令的。我们先用man strings来看看:

       strings - print the strings of printable characters in files.  
 
       意思是, 打印文件中可打印的字符。  我来补充一下吧, 这个文件可以是文本文件(test.c), 可执行文件(test),  动态链接库(test.o), 静态链接库(test.a)
  
       
        脱离代码地长篇大论而不去实际验证, 不是我的风格。 还是搞点代码下菜吧(代码存在test.c中):
 #include <stdio.h>

 int add(int x, int y)
{
return x + y;
} int main()
{
int a = ;
int b = ;
int c = add(a, b);
printf("oh, my dear, c is %d\n", c); return ;
}

我们来看看strings test.c的结果:

 [taoge@localhost learn_c]$ strings test.c
#include <stdio.h>
int add(int x, int y)
return x + y;
int main()
int a = ;
int b = ;
int c = add(a, b);
printf("oh, my dear, c is %d\n", c);
return ;
[taoge@localhost learn_c]$
可以看到, 确实打印出了test.c中的很多字符。
 
 
      下面, 我们对可执行文件用strings试试, 如下:
 [taoge@localhost learn_c]$ gcc test.c
[taoge@localhost learn_c]$ strings a.out
/lib/ld-linux.so.
=$TsU
__gmon_start__
libc.so.
_IO_stdin_used
printf
__libc_start_main
GLIBC_2.
PTRh
[^_]
oh, my dear, c is %d
[taoge@localhost learn_c]$
以看到, 打印出了a.out中很多字符。
 
 
       实际上, 如果有目标文件、静态库或动态库, , 也是可以用strings命令进行打印操作的。 我们来看看:
       xxx.h文件:
 
 void print();
 #include <stdio.h>
#include "xxx.h" void print()
{
printf("rainy days\n");
}

然后, 我们来看看怎么制作静态、动态库吧(在后续博文中会继续详细介绍):

 [taoge@localhost learn_strings]$ ls
xxx.c xxx.h
[taoge@localhost learn_strings]$ gcc -c xxx.c
[taoge@localhost learn_strings]$ ar rcs libxxx.a xxx.o
[taoge@localhost learn_strings]$ gcc -shared -fPIC -o libxxx.so xxx.o
[taoge@localhost learn_strings]$ ls
libxxx.a libxxx.so xxx.c xxx.h xxx.o
[taoge@localhost learn_strings]$ strings xxx.o
rainy days
[taoge@localhost learn_strings]$ strings libxxx.a
!<arch>
/ `
Rprint
xxx.o/ `
rainy days
GCC: (GNU) 4.4. (Red Hat 4.4.-)
.symtab
.strtab
.shstrtab
.rel.text
.data
.bss
.rodata
.comment
.note.GNU-stack
xxx.c
print
puts
[taoge@localhost learn_strings]$
[taoge@localhost learn_strings]$
[taoge@localhost learn_strings]$ strings libxxx.so
__gmon_start__
_init
_fini
__cxa_finalize
_Jv_RegisterClasses
print
puts
libc.so.
_edata
__bss_start
_end
GLIBC_2.1.3
GLIBC_2.
rainy days
[taoge@localhost learn_strings]$
看到了吧。
 
 
       strings命令很简单, 看起来好像没什么, 但实际有很多用途。 下面, 我来举一个例子。  在大型的软件开发中, 假设有100个.c/.cpp文件, 这个.cpp文件最终生成10个.so库, 那么怎样才能快速知道某个.c/.cpp文件编译到那个.so库中去了呢? 当然, 你可能要说, 看makefile不就知道了。 对, 看makefile肯定可以, 但如下方法更好, 直接用命令:
      strings -f "*.so" | grep "xxxxxx"
 
      如果还不明白, 那就就以上面的小程序为例为说明, 不过, 此处我们考虑所有的文件, 如下:
 [taoge@localhost learn_c]$ strings -f * | grep "my dear"
a.out: oh, my dear, c is %d
test.c: printf("oh, my dear, c is %d\n", c);
[taoge@localhost learn_c]$
可以看到, 源文件test.c和可执行文件中皆有"my dear"串, 一下子就找到了对应的文件,清楚了吧。如果某.c/.cpp文件编译进了.so库, 那么,strings -f * | grep "my dear"必定可以找到对应的.so文件, 其中"my dear"为该.c/.cpp文件中的某个日志串(比如以printf为打印)。
 
 
       strings的作用先介绍到此, 算是抛砖引玉地熟悉一下strings吧。

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

  1. linux中的strings命令简介2

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

  2. linux中的ldd命令简介

    转载自:http://blog.csdn.net/stpeace/article/details/47069215 在linux中, 有些命令是大家通用的, 比如ls, rm, mv, cp等等, 这 ...

  3. linux中的nm命令简介

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

  4. linux中的strings命令

     strings - print the strings of printable characters in files.            意思是, 打印文件中可打印的字符.  我来补充一下吧 ...

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

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

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

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

  7. linux中的strip命令简介

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

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

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

  9. Linux中的历史命令

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

随机推荐

  1. UESTC_吴队长征婚 2015 UESTC Training for Search Algorithm & String<Problem E>

    E - 吴队长征婚 Time Limit: 10000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  2. Binary Tree Paths 解答

    Question Given a binary tree, return all root-to-leaf paths. For example, given the following binary ...

  3. devStack

    1,devstack shell 脚本开源官网 http://devstack.org/ 脚本功能快速搭建 OpenStack 的运行和开发环境 [Note tips by Ruiy devstack ...

  4. SVN强制填写日志

    在F:\Repositories\版本库名\hooks下新建pre-commit.bat 内容如下: @echo off setlocal set SVN_BINDIR="C:\Progra ...

  5. NSNotificationCenter 传对象

    [[NSNotificationCenter defaultCenter] postNotificationName:@"postCity" object:pro]; [[NSNo ...

  6. IOS 计算密码强度

    + (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...

  7. HDU 4508 湫湫系列故事——减肥记I(全然背包)

    HDU 4508 湫湫系列故事--减肥记I(全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=4508 题意: 有n种食物, 每种食物吃了能获得val[i ...

  8. Cocos2d-x游戏开发CCBAnimationManager控制动画

    CocosBuilder能方便的编辑各种动画.大部分动画都是以独立片段的形式存在的. 须要由程序来控制何时播放. 管理ccbi文件的动画播放有个专门的类:CCBAnimationManager 大致的 ...

  9. 计算机与ARM板通过路由器相连

    首先,使用两根网线分别将计算机和ARM板与路由器的LAN口连接. 要想使计算机和ARM板通信,必须使二者在同一网段. 在计算机的终端输入:ifconfig 获取计算机的ip地址,查看eth0,我的ip ...

  10. pt-online-schema-change解读

    [用途]在线改表 [注意风险]因为涉及到修改表的数据和结构,所以在使用前要小心测试并做好备份,工具默认不会改表,除非你添加了--execute参数 [工具简介] pt-osc模仿MySQL内部的改表方 ...