Does gnu ld link in whole object files or only the needed functions?

十年热恋 提交于 2019-12-10 09:26:11

问题


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.

According to avr-libc's documentation:

the linker links in THE ENTIRE OBJECT MODULE in which the function is located

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/13525889/does-gnu-ld-link-in-whole-object-files-or-only-the-needed-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!