前言:在Linux下开发应用程序时,经常遇到编译后的软件包过大,在我们发布正式的软件版本前,需要做一下瘦身,这时需要用到strip命令了。
strip介绍
简单的说,strip命令就是给文件脱掉外衣,具体就是从特定文件中剥掉一些符号信息和调试信息,使文件大小变小,下面是man信息。
NAME
strip - Discard symbols from object files.
SYNOPSIS
strip [-F bfdname |–target=bfdname]
[-I bfdname |–input-target=bfdname]
[-O bfdname |–output-target=bfdname]
[-s|–strip-all]
[-S|-g|-d|–strip-debug]
[–strip-dwo]
[-K symbolname|–keep-symbol=symbolname]
[-M|–merge-notes][–no-merge-notes]
[-N symbolname |–strip-symbol=symbolname]
[-w|–wildcard]
[-x|–discard-all] [-X |–discard-locals]
[-R sectionname |–remove-section=sectionname]
[–remove-relocations=sectionpattern]
[-o file] [-p|–preserve-dates]
[-D|–enable-deterministic-archives]
[-U|–disable-deterministic-archives]
[–keep-file-symbols]
[–only-keep-debug]
[-v |–verbose] [-V|–version]
[–help] [–info]
objfile…
DESCRIPTION
GNU strip discards all symbols from object files objfile. The list of object files may include archives. At
least one object file must be given.
strip modifies the files named in its argument, rather than writing modified copies under different names.
实际测试
编写如下代码:
// @file: hello.c
#include <stdio.h>
int main()
{
for(int i = 0; i < 3; ++i) {
printf("Hello World\n");
}
return 0;
}
接下来 gcc hello.c -o hello 编译,ls -al 查看文件大小和 nm 查看符号表信息:
然后执行 strip -s hello 对文件进行瘦身,查看文件大小和符号表信息作对比如下: