Linux下的strip命令学习

strip

strip是Linux下的一个命令。可以用于给应用脱衣服,帮助我们抹除一些调试信息。(虽然不知道具体是什么,但是会用就好了
在嵌入式开发领域用到的应该比较多
首先,先写一个示例看看

//hello.cpp
#include <iostream>

using namespace std;
int main() {
    int i = 10;
    for(;i > 0;i--) {
        cout<<"hello,i = "<<(10 - i)<<endl;
    }
    return 0;
}

好了,编译一下,看下大小

 $ g++ hello.cpp -o hello.out
 $ ls -l
total 16
-rw-r--r-- 1 root root  171 Jul 12  3:00 hello.cpp
-rwxr-xr-x 1 root root 8968 Jul 12  3:00 hello.out
 $ 

我们可以看到大小是8k左右,那么我们来strip看下

 $ strip hello.out -o hello.strip.out
 $ ls -l
total 24
-rw-r--r-- 1 root root  171 Jul 12  3:00 hello.cpp
-rwxr-xr-x 1 root root 8968 Jul 12  3:00 hello.out
-rwxr-xr-x 1 root root 6120 Jul 12  3:10 hello.strip.out
 $ 

很好,我们可以看到文件明显减少了许多,现在是一个6k左右的文件
6120÷8968≈0.6824=68.24%
1-68.24%=31.76%
减少了大概31.76%
嗯... 应该是加的库越多可以减少的越多吧。
那么我们来看看能不能正常运行

 $ ./hello.strip.out
hello,i = 0
hello,i = 1
hello,i = 2
hello,i = 3
hello,i = 4
hello,i = 5
hello,i = 6
hello,i = 7
hello,i = 8
hello,i = 9
 $ 

我们看下strip都有那些用法。

 $ strip --help
Usage: strip <option(s)> in-file(s)
 Removes symbols and sections from files
 The options are:
  -I --input-target=<bfdname>      Assume input file is in format <bfdname>
  -O --output-target=<bfdname>     Create an output file in format <bfdname>
  -F --target=<bfdname>            Set both input and output format to <bfdname>
  -p --preserve-dates              Copy modified/access timestamps to the output
  -D --enable-deterministic-archives
                                   Produce deterministic output when stripping archives (default)
  -U --disable-deterministic-archives
                                   Disable -D behavior
  -R --remove-section=<name>       Also remove section <name> from the output
     --remove-relocations <name>   Remove relocations from section <name>
  -s                       Remove all symbol and relocation information
  -g -S -d --strip-debug           Remove all debugging symbols & sections
     --strip-dwo                   Remove all DWO sections
     --strip-unneeded              Remove all symbols not needed by relocations
     --only-keep-debug             Strip everything but the debug information
... #此处省略

那么我们可以看到,常见的有-I和-O,也就是输入和输出啦。发现一个不对的地方,它这里的O应该是小写的,大写的O会报错
然后是 -F format,设置输入和输出文件的格式
-p 保护日期,那么这个应该是不修改原来文件的modified的日期的吧
-D 和-U 需要一起讲,D应该就是它这个deterministic的意思吧,确定性的。那么-D就是产生确定性输出,-U就是不产生确定性输出。
-R 可以删除指定的section
-s 表示可以strip-all。
后面太多了,大家可以自行琢磨

嗯,大概就这些吧。正常使用的strip filename -o output_filename 就应该完全够用。
以上就是本文的全部内容,有什么疑问欢迎来一起探讨。

那么我们来看下加了s之后的文件大小是多少

 $ strip hello.out -p -s -o hello.s.out
 $ ls -l
total 32
-rw-r--r-- 1 root root  171 Jul 12  3:00 hello.cpp
-rwxr-xr-x 1 root root 8968 Jul 12  3:00 hello.out
-rwxr-xr-x 1 root root 6120 Jul 12  3:00 hello.s.out
-rwxr-xr-x 1 root root 6120 Jul 12  3:10 hello.strip.out
 $ 

哈哈,看来默认就是有-s的了

posted @   '昵称'undeclared  阅读(1708)  评论(1编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示