gnu ld是链接在整个目标文件中还是仅链接所需的功能?

问题描述:

我们有一个库和一个可执行文件,该文件将被静态链接到库.我们希望最小化最终可执行文件的程序空间.

We have a library and an executable, that is to be statically linked to the lib. We want to minimize the program space of the final executable.

根据avr-libc的文档:

According to avr-libc's documentation:

该功能所在的整个对象模块中的链接器链接

另一方面,我的同事们一致认为,链接器会丢弃一些未使用的功能.

On the other hand, my colleagues are unanimous on the point that at some pass, the linker throws away any unused functions.

那么谁是正确的,或者我误会了什么?在整个gcc中答案是一致的吗?还是我们在这里只说avr端口?

So who is correct or am I misunderstanding something? Is the answer consistent throughout gcc or are we talking only the avr port here?

除非您告知,否则它不会执行无效代码剥离.为此,您需要使用以下命令编译所有内容:

It doesn't perform dead code stripping unless you tell it to. In order to do that, you need to compile everything with:

-fdata-sections -ffunction-sections

以标记所有数据和功能.与GCC链接时,您需要通过:

in order to mark all data and functions. And when linking with GCC you need to pass:

-Wl,--gc-sections

以便对所有未使用的部分进行垃圾收集.

in order to garbage-collect all unused sections.