问题描述
我要狠狠优化我的可执行文件的大小( ARM
开发)和
我注意到,在我目前的建设方案( GCC
+ LD
)未使用的符号都没有得到剥离。
I need to optimize the size of my executable severely (ARM
development) andI noticed that in my current build scheme (gcc
+ ld
) unused symbols are not getting stripped.
的使用量臂带--strip-不需要
为生成的可执行文件/库不改变可执行文件的输出大小的(我有不知道为什么,也许它根本不能)的
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).
会是什么方式的(如果它存在)的修改我建筑管道,使得未使用的符号是从所得到的文件剥离
What would be the way (if it exists) to modify my building pipeline, so that the unused symbols are stripped from the resulting file?
我就别想这一点,但我目前的嵌入式环境是不是很厉害,并
节约了 500K
出 2M
在一个非常漂亮的装载性能提升的结果。
I wouldn't even think of this, but my current embedded environment isn't very "powerful" andsaving even 500K
out of 2M
results in a very nice loading performance boost.
更新:
不幸的是,当前的 GCC
版本我用不具备 -dead带钢
选项和 -ffunction截面... + --gc截面
为 LD
没有给出结果输出任何显著差异
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.
的我很震惊,这甚至成为一个问题,因为我确信 GCC + LD
应自动剥离未使用的符号(为什么他们甚至有让他们?)。的
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?).
推荐答案
有关GCC,这是分两个阶段完成:
For GCC, this is accomplished in two stages:
首先编译数据,而是告诉编译器翻译单元内分开code到单独的章节。这将为函数,类和外部变量通过使用以下两种编译器标志来完成:
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
链接翻译单位共同使用的连接器优化标志(这会导致链接放弃未引用部分):
Link the translation units together using the linker optimization flag (this causes the linker to discard unreferenced sections):
-Wl,--gc-sections
所以,如果你有一个文件调用TEST.CPP了曾在其声明的两个功能,但其中一人是未使用的,你可以省略未使用的用下面的命令来GCC(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是一个额外的编译标志,告诉GCC以优化尺寸)
(Note that -Os is an additional compiler flag that tells GCC to optimize for size)
这篇关于如何使用GCC和LD删除未使用的C / C ++的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!