zoukankan      html  css  js  c++  java
  • 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的了

  • 相关阅读:
    动手做第一个Chrome插件
    Discuz NT 架构剖析之Config机制
    用游标实现查询当前服务器所有数据库所有表的SQL
    Discuz X3.2 网站快照被劫持的解决方法
    centos下MYSQL 没有ROOT用户的解决方法。
    redis命令1
    在当今快节奏的软件更迭当中,我们是否还需要进行系统的学习?
    StructureMap 代码分析之Widget 之Registry 分析 (1)
    C#面试题汇总(未完成)
    C#:解决WCF中服务引用 自动生成代码不全的问题。
  • 原文地址:https://www.cnblogs.com/nbtech/p/13287918.html
Copyright © 2011-2022 走看看