如何使用 GCC 和 ld 删除未使用的 C/C++ 符号?

时间:2022-11-19
本文介绍了如何使用 GCC 和 ld 删除未使用的 C/C++ 符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要严格优化我的可执行文件的大小(ARM 开发)并且我注意到在我当前的构建方案 (gcc + ld) 中未使用的符号没有被剥离.

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 用于生成的可执行文件/库不会改变可执行文件的输出大小 (我不知道为什么,也许是根本不能).

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" and saving even 500K out of 2M results in a very nice loading performance boost.

更新:

不幸的是,我使用的当前 gcc 版本没有 -dead-strip 选项和 -ffunction-sections... + --gcld 的 -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.

我很震惊这甚至成为一个问题,因为我确信 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:

首先编译数据,但告诉编译器将代码分成翻译单元内的单独部分.这将通过使用以下两个编译器标志为函数、类和外部变量完成:

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++ 符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:提取文本 OpenCV 下一篇:查找C++静态初始化顺序问题

相关文章

最新文章