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的了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM