当前位置:首页 > 操作系统 > Linux

linux中的strings命令简介

摘自: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中):
                 1 #include <stdio.h>
 2 3int add(int x, int y)
 4{
 5return x + y;
 6}
 7 8int main()
 9{
10int a = 1;
11int b = 2;
12int c = add(a, b);
13         printf("oh, my dear, c is %dn", c);
1415return0;
16 }

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

                 1
                [taoge@localhost learn_c]$ strings test.c 

                 2 #include <stdio.h>
 3int add(int x, int y)
 4return x + y;
 5int main()
 6int a = 1;
 7int b = 2;
 8int c = add(a, b);
 9     printf("oh, my dear, c is %dn", c);
10return0;
11 [taoge@localhost learn_c]$ 
可以看到, 确实打印出了test.c中的很多字符。
 
 
      下面, 我们对可执行文件用strings试试, 如下:
                     1
                    [taoge@localhost learn_c]$ gcc test.c 

                     2 [taoge@localhost learn_c]$ strings a.out 3 /lib/ld-linux.so.2 4 =$TsU
 5__gmon_start__
 6 libc.so.6 7_IO_stdin_used
 8printf
 9__libc_start_main
10 GLIBC_2.011PTRh 
12 [^_]
13 oh, my dear, c is %d
14 [taoge@localhost learn_c]$ 
以看到, 打印出了a.out中很多字符。
 
 
       实际上, 如果有目标文件、静态库或动态库, , 也是可以用strings命令进行打印操作的。 我们来看看:
       xxx.h文件:
 
                        1
                        void print();
                        1 #include <stdio.h>
2 #include "xxx.h"34void print()
5{
6     printf("rainy daysn");
7 }

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

                         1
                        [taoge@localhost learn_strings]$ ls

                         2
                        xxx.c  xxx.h

                         3 [taoge@localhost learn_strings]$ gcc -c xxx.c
 4[taoge@localhost learn_strings]$ ar rcs libxxx.a xxx.o
 5 [taoge@localhost learn_strings]$ gcc -shared -fPIC -o libxxx.so xxx.o
 6[taoge@localhost learn_strings]$ ls
 7libxxx.a  libxxx.so  xxx.c  xxx.h  xxx.o
 8[taoge@localhost learn_strings]$ strings xxx.o
 9rainy days
10[taoge@localhost learn_strings]$ strings libxxx.a
11 !<arch>
12 /               143788733900014        `
13Rprint
14 xxx.o/          1437887333501502100664848       `
15rainy days
16 GCC: (GNU) 4.4.420100726 (Red Hat 4.4.4-13)
17.symtab
18.strtab
19.shstrtab
20.rel.text
21.data
22.bss
23.rodata
24.comment
25 .note.GNU-stack
26xxx.c
27print
28puts
29[taoge@localhost learn_strings]$ 
30[taoge@localhost learn_strings]$ 
31[taoge@localhost learn_strings]$ strings libxxx.so
32__gmon_start__
33_init
34_fini
35__cxa_finalize
36_Jv_RegisterClasses
37print
38puts
39 libc.so.640_edata
41__bss_start
42_end
43 GLIBC_2.1.344 GLIBC_2.045rainy days
46 [taoge@localhost learn_strings]$ 
看到了吧。
 
 
       strings命令很简单, 看起来好像没什么, 但实际有很多用途。 下面, 我来举一个例子。  在大型的软件开发中, 假设有100个.c/.cpp文件, 这个.cpp文件最终生成10个.so库, 那么怎样才能快速知道某个.c/.cpp文件编译到那个.so库中去了呢? 当然, 你可能要说, 看makefile不就知道了。 对, 看makefile肯定可以, 但如下方法更好, 直接用命令:
      strings -f "*.so" | grep "xxxxxx"
 
      如果还不明白, 那就就以上面的小程序为例为说明, 不过, 此处我们考虑所有的文件, 如下:
                            1 [taoge@localhost learn_c]$ strings -f * | grep "my dear"2 a.out: oh, my dear, c is %d
3 test.c:     printf("oh, my dear, c is %dn", c);
4 [taoge@localhost learn_c]$ 
可以看到, 源文件test.c和可执行文件中皆有"my dear"串, 一下子就找到了对应的文件,清楚了吧。如果某.c/.cpp文件编译进了.so库, 那么,strings -f * | grep "my dear"必定可以找到对应的.so文件, 其中"my dear"为该.c/.cpp文件中的某个日志串(比如以printf为打印)。
 
 
       strings的作用先介绍到此, 算是抛砖引玉地熟悉一下strings吧。

 

 

 

 

原文:http://www.cnblogs.com/LiuYanYGZ/p/5574595.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐