与静态库与单个目标文件的链接 [英] link with static library vs individual object files

查看:21
本文介绍了与静态库与单个目标文件的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我想将静态库 (libx.a) 解压缩到单独的目标文件 (ao bo co) 中,并在链接器输入列表中指定这些目标文件 (ao bo co) 而不是 libx.a,使用其他链接器选项保持不变.

For a reason i want to unpack a static lib (libx.a) into individual object files (a.o b.o c.o), and specify these object files (a.o b.o c.o) in the linker input list instead of libx.a, with other linker options remaining the same.

但是,我注意到上述更改导致输出可执行文件存在相当大的差异.基本上,(a.o b.o c.o) 方法会导致更大的输出大小.

However, i have noticed the above change has resulted in quite some difference in the output executable. Basically, (a.o b.o c.o) method will result in larger output size.

那么这两种方法(libx.a 和单个目标文件)有什么区别?有没有办法解决?

So what's the difference between the two methods (libx.a and individual object files)? And is there a way to work around?

我使用的 GNU binutil (for and ar ld) 版本是 2.16.1

The GNU binutil (for and ar ld) version i'm using is 2.16.1

谢谢.

推荐答案

Ld 删除链接的 .lib 档案中未使用的部分(如具有全局链接的变量).当直接传递目标文件时,无法进行此优化,因为链接器无法确定 .o 文件的某些未引用元素是否需要稍后某个未知部分(例如,因为它对模块导出列表是外部可见的)或可以完全删除.当在链接过程中放置​​ .lib 时,链接器肯定知道它可以删除不必要的元素.

Ld removes unused parts of linked .lib archives (like variables with global linkage). This optimization cannot take place when the object files are passed directly, since the linker cannot determine if some unreferenced element of an .o file is needed by some unknown part later (for example because it would be extern visible by the module export list) or can be removed entirely. When a .lib is put in place in the linking process the linker knows for sure that it can drop unnecessary elements.

这篇关于与静态库与单个目标文件的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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