詳解linux中的strings命令簡介_Linux

來源:互聯網
上載者:User

在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_used printf __libc_start_main GLIBC_2.0 PTRh  [^_] 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]$ 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> /        1437887339 0   0   0    14    ` Rprint xxx.o/     1437887333 501  502  100664 848    ` rainy days GCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13) .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.6 _edata __bss_start _end GLIBC_2.1.3 GLIBC_2.0 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吧。

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.