問(wèn)題描述
我需要嚴(yán)格優(yōu)化我的可執(zhí)行文件的大小(ARM
開(kāi)發(fā))并且我注意到在我當(dāng)前的構(gòu)建方案 (gcc
+ ld
) 中未使用的符號(hào)沒(méi)有被剝離.
I need to optimize the size of my executable severely (ARM
development) and
I noticed that in my current build scheme (gcc
+ ld
) unused symbols are not getting stripped.
將 arm-strip --strip-unneeded
用于生成的可執(zhí)行文件/庫(kù)不會(huì)改變可執(zhí)行文件的輸出大小 (我不知道為什么,也許是根本不能).
The usage of the arm-strip --strip-unneeded
for the resulting executables / libraries doesn't change the output size of the executable (I have no idea why, maybe it simply can't).
如何(如果存在)修改我的構(gòu)建管道,以便從生成的文件中刪除未使用的符號(hào)?
What would be the way (if it exists) to modify my building pipeline, so that the unused symbols are stripped from the resulting file?
我什至不會(huì)想到這一點(diǎn),但我當(dāng)前的嵌入式環(huán)境不是很強(qiáng)大",而且甚至將 500K
保存在 2M
中,都會(huì)帶來(lái)非常好的加載性能提升.
I wouldn't even think of this, but my current embedded environment isn't very "powerful" and
saving even 500K
out of 2M
results in a very nice loading performance boost.
更新:
不幸的是,我使用的當(dāng)前 gcc
版本沒(méi)有 -dead-strip
選項(xiàng)和 -ffunction-sections... + --gc
對(duì)結(jié)果輸出沒(méi)有任何顯著差異.ld
的 -sections
Unfortunately the current gcc
version I use doesn't have the -dead-strip
option and the -ffunction-sections... + --gc-sections
for ld
doesn't give any significant difference for the resulting output.
我很震驚這甚至成為一個(gè)問(wèn)題,因?yàn)槲掖_信 gcc + ld
應(yīng)該自動(dòng)去除未使用的符號(hào)(為什么他們甚至必須保留它們?).
I'm shocked that this even became a problem, because I was sure that gcc + ld
should automatically strip unused symbols (why do they even have to keep them?).
推薦答案
對(duì)于 GCC,這分兩個(gè)階段完成:
For GCC, this is accomplished in two stages:
首先編譯數(shù)據(jù),但告訴編譯器將代碼分成翻譯單元內(nèi)的單獨(dú)部分.這將通過(guò)使用以下兩個(gè)編譯器標(biāo)志為函數(shù)、類和外部變量完成:
First compile the data but tell the compiler to separate the code into separate sections within the translation unit. This will be done for functions, classes, and external variables by using the following two compiler flags:
-fdata-sections -ffunction-sections
使用鏈接器優(yōu)化標(biāo)志將翻譯單元鏈接在一起(這會(huì)導(dǎo)致鏈接器丟棄未引用的部分):
Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):
-Wl,--gc-sections
因此,如果您有一個(gè)名為 test.cpp 的文件,其中聲明了兩個(gè)函數(shù),但其??中一個(gè)未使用,您可以使用以下 gcc(g++) 命令省略未使用的那個(gè):
So if you had one file called test.cpp that had two functions declared in it, but one of them was unused, you could omit the unused one with the following command to gcc(g++):
gcc -Os -fdata-sections -ffunction-sections test.cpp -o test -Wl,--gc-sections
(注意 -Os 是一個(gè)額外的編譯器標(biāo)志,它告訴 GCC 優(yōu)化大小)
(Note that -Os is an additional compiler flag that tells GCC to optimize for size)
這篇關(guān)于如何使用 GCC 和 ld 刪除未使用的 C/C++ 符號(hào)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!