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

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

问题描述

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

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和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天全站免登陆