詳解linux中的strings命令簡介
在Linux下搞軟件開發(fā)的朋友, 幾乎沒有不知道strings命令的。我們先用man strings來看看:
strings - print the strings of printable characters in files.
意思是, 打印文件中可打印的字符。 我來補(bǔ)充一下吧, 這個(gè)文件可以是文本文件(test.c), 可執(zhí)行文件(test), 動(dòng)態(tài)鏈接庫(test.o), 靜態(tài)鏈接庫(test.a)
脫離代碼地長篇大論而不去實(shí)際驗(yàn)證, 不是我的風(fēng)格。 還是搞點(diǎn)代碼下菜吧(代碼存在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的結(jié)果:
[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]$
可以看到, 確實(shí)打印出了test.c中的很多字符。
下面, 我們對(duì)可執(zhí)行文件用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中很多字符。
實(shí)際上, 如果有目標(biāo)文件、靜態(tài)庫或動(dòng)態(tài)庫, , 也是可以用strings命令進(jìn)行打印操作的。 我們來看看:
xxx.h文件:
void print();
xxx.c文件:
#include <stdio.h> #include "xxx.h" void print() { printf("rainy days\n"); }
然后, 我們來看看怎么制作靜態(tài)、動(dòng)態(tài)庫吧(在后續(xù)博文中會(huì)繼續(xù)詳細(xì)介紹):
[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命令很簡單, 看起來好像沒什么, 但實(shí)際有很多用途。 下面, 我來舉一個(gè)例子。 在大型的軟件開發(fā)中, 假設(shè)有100個(gè).c/.cpp文件, 這個(gè).cpp文件最終生成10個(gè).so庫, 那么怎樣才能快速知道某個(gè).c/.cpp文件編譯到那個(gè).so庫中去了呢? 當(dāng)然, 你可能要說, 看makefile不就知道了。 對(duì), 看makefile肯定可以, 但如下方法更好, 直接用命令:
strings -f "*.so" | grep "xxxxxx"
如果還不明白, 那就就以上面的小程序?yàn)槔秊檎f明, 不過, 此處我們考慮所有的文件, 如下:
[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和可執(zhí)行文件中皆有"my dear"串, 一下子就找到了對(duì)應(yīng)的文件,清楚了吧。如果某.c/.cpp文件編譯進(jìn)了.so庫, 那么,strings -f * | grep "my dear"必定可以找到對(duì)應(yīng)的.so文件, 其中"my dear"為該.c/.cpp文件中的某個(gè)日志串(比如以printf為打印)。
strings的作用先介紹到此, 算是拋磚引玉地熟悉一下strings吧。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mac下配置和訪問阿里云服務(wù)器(Ubuntu系統(tǒng))的圖文教程
這篇文章主要介紹了mac下配置和訪問阿里云服務(wù)器(Ubuntu系統(tǒng))的圖文教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01Linux用戶自定義at、cron計(jì)劃任務(wù)執(zhí)行的方法
今天小編就為大家分享一篇Linux用戶自定義at、cron計(jì)劃任務(wù)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07vim配置顯示行號(hào)和語法高亮 即.vimrc文件的配置
本文主要介紹vim配置顯示行號(hào)和語法高亮的功能,比較實(shí)用,需要的朋友可以參考一下。2016-06-06