gnu ld是链接在整个目标文件中还是仅链接所需的功能? [英] Does gnu ld link in whole object files or only the needed functions?

查看:111
本文介绍了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.

这篇关于gnu ld是链接在整个目标文件中还是仅链接所需的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆