linux中的strings命令简介

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

       在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 = 1;        int b = 2;        int c = add(a, b);        printf("oh, my dear, c is %d\n", c);        return 0;}
       我们来看看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 = 1; int b = 2; int c = add(a, b); printf("oh, my dear, c is %d\n", c); return 0;[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.2=$TsU__gmon_start__libc.so.6_IO_stdin_usedprintf__libc_start_mainGLIBC_2.0PTRh [^_]oh, my dear, c is %d[taoge@localhost learn_c]$ 
       可以看到, 打印出了a.out中很多字符。


       实际上, 如果有目标文件、静态库或动态库, , 也是可以用strings命令进行打印操作的。 我们来看看:
       xxx.h文件:
void print();
       xxx.c文件:
#include <stdio.h>#include "xxx.h"void print()printf("rainy days\n");}
       然后, 我们来看看怎么制作静态、动态库吧(在后续博文中会继续详细介绍):
[taoge@localhost learn_strings]$ lsxxx.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]$ lslibxxx.a  libxxx.so  xxx.c  xxx.h  xxx.o[taoge@localhost learn_strings]$ strings xxx.orainy days[taoge@localhost learn_strings]$ strings libxxx.a!<arch>/               1437887339  0     0     0       14        `Rprintxxx.o/          1437887333  501   502   100664  848       `rainy daysGCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13).symtab.strtab.shstrtab.rel.text.data.bss.rodata.comment.note.GNU-stackxxx.cprintputs[taoge@localhost learn_strings]$ [taoge@localhost learn_strings]$ [taoge@localhost learn_strings]$ strings libxxx.so__gmon_start___init_fini__cxa_finalize_Jv_RegisterClassesprintputslibc.so.6_edata__bss_start_endGLIBC_2.1.3GLIBC_2.0rainy 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 %dtest.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吧。



       






           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值