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

摘要:
标准时间>inty){returnx+y;charzTest[]=“good”;intmain(){intccc=2;return0;

转载于:http://blog.csdn.net/stpeace/article/details/47090255

作为一名linux开发人员, 如果没有听说过strip命令, 那是很不应该的。 strip这个单词, 大家应该早就学过了, 你就记住是脱衣服就行了, 别的不要多想。 在linux中, strip也有脱衣服的含义, 具体就是从特定文件中剥掉一些符号信息和调试信息。

        我们来看main.c文件:

  1. #include <stdio.h>  
  2.   
  3. int add(int x, int y)  
  4. {  
  5.     return x + y;  
  6. }  
  7.   
  8. int aaa;  
  9. int bbb = 1;  
  10. char szTest[] = "good";  
  11.   
  12. int main()  
  13. {  
  14.     int ccc = 2;  
  15.     return 0;  
  16. }  
#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;
}

       然后我们看看结果:

  1. [taoge@localhost learn_strip]$ ls  
  2. main.c  
  3. [taoge@localhost learn_strip]$ gcc main.c   
  4. [taoge@localhost learn_strip]$ ls -l a.out   
  5. -rwxrwxr-x 1 taoge taoge 4673 Jul 27 05:30 a.out  
  6. [taoge@localhost learn_strip]$ file a.out   
  7. 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  
  8. [taoge@localhost learn_strip]$ nm a.out   
  9. 08049538 d _DYNAMIC  
  10. 08049604 d _GLOBAL_OFFSET_TABLE_  
  11. 0804847c R _IO_stdin_used  
  12.          w _Jv_RegisterClasses  
  13. 08049528 d __CTOR_END__  
  14. 08049524 d __CTOR_LIST__  
  15. 08049530 D __DTOR_END__  
  16. 0804952c d __DTOR_LIST__  
  17. 08048520 r __FRAME_END__  
  18. 08049534 d __JCR_END__  
  19. 08049534 d __JCR_LIST__  
  20. 08049628 A __bss_start  
  21. 08049618 D __data_start  
  22. 08048430 t __do_global_ctors_aux  
  23. 08048310 t __do_global_dtors_aux  
  24. 08048480 R __dso_handle  
  25.          w __gmon_start__  
  26. 0804842a T __i686.get_pc_thunk.bx  
  27. 08049524 d __init_array_end  
  28. 08049524 d __init_array_start  
  29. 080483c0 T __libc_csu_fini  
  30. 080483d0 T __libc_csu_init  
  31.          U __libc_start_main@@GLIBC_2.0  
  32. 08049628 A _edata  
  33. 08049634 A _end  
  34. 0804845c T _fini  
  35. 08048478 R _fp_hw  
  36. 08048274 T _init  
  37. 080482e0 T _start  
  38. 08049630 B aaa  
  39. 08048394 T add  
  40. 0804961c D bbb  
  41. 08049628 b completed.5963  
  42. 08049618 W data_start  
  43. 0804962c b dtor_idx.5965  
  44. 08048370 t frame_dummy  
  45. 080483a2 T main  
  46. 08049620 D szTest  
  47. [taoge@localhost learn_strip]$   
[taoge@localhost learn_strip]$ ls
main.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 _DYNAMIC
08049604 d _GLOBAL_OFFSET_TABLE_
0804847c R _IO_stdin_used
         w _Jv_RegisterClasses
08049528 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_start
08049618 D __data_start
08048430 t __do_global_ctors_aux
08048310 t __do_global_dtors_aux
08048480 R __dso_handle
         w __gmon_start__
0804842a T __i686.get_pc_thunk.bx
08049524 d __init_array_end
08049524 d __init_array_start
080483c0 T __libc_csu_fini
080483d0 T __libc_csu_init
         U __libc_start_main@@GLIBC_2.0
08049628 A _edata
08049634 A _end
0804845c T _fini
08048478 R _fp_hw
08048274 T _init
080482e0 T _start
08049630 B aaa
08048394 T add
0804961c D bbb
08049628 b completed.5963
08049618 W data_start
0804962c b dtor_idx.5965
08048370 t frame_dummy
080483a2 T main
08049620 D szTest
[taoge@localhost learn_strip]$ 

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

       通过file命令可知, a.out是可执行文件, 且是not stripped, 也就是说没有脱衣服。

       通过nm命令, 可以读出a.out中的符号信息。

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

  1. [taoge@localhost learn_strip]$ ls  
  2. a.out  main.c  
  3. [taoge@localhost learn_strip]$ strip a.out   
  4. [taoge@localhost learn_strip]$ ls -l a.out   
  5. -rwxrwxr-x 1 taoge taoge 2980 Jul 27 05:34 a.out  
  6. [taoge@localhost learn_strip]$ file a.out   
  7. a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped  
  8. [taoge@localhost learn_strip]$ nm a.out   
  9. nm: a.out: no symbols  
  10. [taoge@localhost learn_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来搞一下吧。

免责声明:文章转载自《linux中的strip命令简介------给文件脱衣服》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇awk简介(使用方法)Network Configuration笔记下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux 内核编译步骤及配置详解

linux 系统体系结构: linux kernel体系结构:arm有7种工作模式,x86也实现了4个不同级别RING0-RING3,RING0级别最高,这样linux用户代码运行在RING3下,内核运行在RING0,这样系统本身就得到了充分的保护 用户空间(用户模式)转到内核空间(系统模式)方法:·系统调用·硬件中断 linux kernel 体系结构...

OCR数据合成工具Text Recognition Data Generator的help文档翻译

   -h, --help            show this help message and exit      --output_dir [OUTPUT_DIR]  The output directory      -i [INPUT_FILE], --input_file [INPUT_FILE]  When set, this argum...

记录一次关于OpenCV的CmakeLists的探索

       编写基于OpenCV的图像处理程序,其中很重要的一道门槛就是编译OpenCV,应该说如果你对其中的内容如果不是很熟悉的话,即使是最简单粗暴的“两次configure,一次generate”都可能会出现各种错误;对于我来说,之前也是停留在能够编译、会解决一些问题阶段,直到前一段时间我需要研究《基于pybind11实现Python调用c++编写...

使用PL/SQL Developer连接远程数据库

附送PL/SQL Developer11中文版下载地址 1、先到Oracle网站下载Instant Client : http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html 根据你的操作系统选择不同的Instant Client版本 下载会是一...

通过Python收集汇聚MySQL 表信息

一.需求 统计收集各个实例上table的信息,主要是表的记录数及大小。 收集的范围是cmdb中所有的数据库实例。 二.公共基础文件说明 1.配置文件 配置文为db_servers_conf.ini,假设cmdb的DBServer为119.119.119.119,单独存放收集监控数据的DBserver为110.110.110.110. 这两个DB实例的访问...

react 取消每次运行项目默认打开浏览器

找到package.json 文件 修改node scripts/start.js 为set BROWSER=none && node scripts/start.js,这样每次启动项目就不会打开浏览器了 "scripts": { "start": "set BROWSER=none && node scripts/...