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

来源:互联网 发布:高速公路收费查询软件 编辑:程序博客网 时间:2024/05/17 12:06
   作为一名linux开发人员, 如果没有听说过strip命令, 那是很不应该的。 strip这个单词, 大家应该早就学过了, 你就记住是脱衣服就行了, 别的不要多想。 在linux中, strip也有脱衣服的含义, 具体就是从特定文件中剥掉一些符号信息。
我们来看main.c文件:

#include <stdio.h>int add(int x, int y){return x + y;}int aaa;int bbb = 1;char szTest[] = "good";int main(){int ccc = 2;return 0;}
然后我们看看结果:

[taoge@localhost learn_strip]$ lsmain.c[taoge@localhost learn_strip]$ gcc main.c [taoge@localhost learn_strip]$ ls -l a.out -rwxrwxr-x 1 taoge taoge 4673 Jul 27 05:30 a.out[taoge@localhost learn_strip]$ file a.out a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped[taoge@localhost learn_strip]$ nm a.out 08049538 d _DYNAMIC08049604 d _GLOBAL_OFFSET_TABLE_0804847c R _IO_stdin_used         w _Jv_RegisterClasses08049528 d __CTOR_END__08049524 d __CTOR_LIST__08049530 D __DTOR_END__0804952c d __DTOR_LIST__08048520 r __FRAME_END__08049534 d __JCR_END__08049534 d __JCR_LIST__08049628 A __bss_start08049618 D __data_start08048430 t __do_global_ctors_aux08048310 t __do_global_dtors_aux08048480 R __dso_handle         w __gmon_start__0804842a T __i686.get_pc_thunk.bx08049524 d __init_array_end08049524 d __init_array_start080483c0 T __libc_csu_fini080483d0 T __libc_csu_init         U __libc_start_main@@GLIBC_2.008049628 A _edata08049634 A _end0804845c T _fini08048478 R _fp_hw08048274 T _init080482e0 T _start08049630 B aaa08048394 T add0804961c D bbb08049628 b completed.596308049618 W data_start0804962c b dtor_idx.596508048370 t frame_dummy080483a2 T main08049620 D szTest[taoge@localhost learn_strip]$ 
通过ls -l 命令可知, a.out的大小是4673个字节;
通过file命令可知, a.out是可执行文件, 且是not stripped, 也就是说没有脱衣服。
通过nm命令, 可以读出a.out中的符号信息。

现在, 我把a.out的衣服strip掉, 得到的结果为:


[taoge@localhost learn_strip]$ ls  a.out  main.c  [taoge@localhost learn_strip]$ strip a.out   [taoge@localhost learn_strip]$ ls -l a.out   -rwxrwxr-x 1 taoge taoge 2980 Jul 27 05:34 a.out  [taoge@localhost learn_strip]$ file a.out   a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped  [taoge@localhost learn_strip]$ nm a.out   nm: a.out: no symbols  [taoge@localhost learn_strip]$ 

通过ls -l 命令可知, a.out的大小是2980个字节, 大大减小;

通过file命令可知, a.out是可执行文件, 且是stripped, 也就是说衣服被脱了;

通过nm命令, 发现a.out中的符号没有了。

     由此可见, strip用于脱掉文件的衣服, 文件会变小, 其中的符号信息会失去。 那这个strip有什么用呢? 很有用的! 原来的a.out比较大, 可以执行。 在strip之后, 文件变小了, 仍然可以执行, 这就就节省了很多空间。

     其实, strip不仅仅可以针对可执行文件, 还能针对目标文件和动态库等。

     在实际的开发中, 经常需要对动态库.so进行strip操作, 减少占地空间。 而在调试的时候(比如用addr2line), 就需要符号了。 因此, 通常的做法是: strip前的库用来调试, strip后的库用来实际发布, 他们两者有对应关系。 一旦发布的strip后的库出了问题, 就可以找对应的未strip的库来定位。

    最后啰嗦一句, 某某动态库strip前是18M左右, strip后是3M左右, 可见, 脱脱衣服还是有明显好处的。

    补充: 后来发现, 在调试过程中, 经常涉及到传库, 库太大时, 很耗费传输时间, 所以还是用strip来搞一下吧。




原创粉丝点击