MSVC编译器之间的关系COMDAT折叠的链接器选项 [英] Relation between MSVC Compiler & linker option for COMDAT folding

查看:170
本文介绍了MSVC编译器之间的关系COMDAT折叠的链接器选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题对SO有一些答案,但是我的稍有不同.在标记为重复项之前,请先试一下.

This question has some answers on SO but mine is slightly different. Before marking as duplicate, please give it a shot.

MSVC始终提供/Gy compiler 选项,以使相同的功能可以折叠到COMDAT部分中.同时,链接器还提供了/OPT:ICF选项.我的理解正确吗,这两个选项必须同时使用?也就是说,前者将功能打包到COMDAT中,而后者消除了冗余COMDAT.正确吗?

MSVC has always provided the /Gy compiler option to enable identical functions to be folded into COMDAT sections. At the same time, the linker also provides the /OPT:ICF option. Is my understanding right that these two options must be used in conjunction? That is, while the former packages functions into COMDAT, the latter eliminates redundant COMDATs. Is that correct?

如果是,那么我们要么同时使用这两个功能,要么将它们都关闭?

If yes, then either we use both or turn off both?

推荐答案

与我离线交流的人的回答.帮助我更好地了解了这些选项.

Answer from someone who communicated with me off-line. Helped me understand these options a lot better.

=================================

===================================

基本上是这样.假设我们只讲C或C ++,但没有成员函数.如果没有/Gy,则编译器将创建某种程度上无法还原的目标文件.如果链接程序只希望对象提供一个功能,则它将全部获取.这是对库进行编程时特别要考虑的一个问题,例如,如果您要对库的用户友好,则应将库编写为许多小对象文件,通常每个对象一个非静态函数,以便使用该库的用户.库不必携带实际上永远不会执行的代码.

That is essentially true. Suppose we talk just C, or C++ but with no member functions. Without /Gy, the compiler creates object files that are in some sense irreducible. If the linker wants just one function from the object, it gets them all. This is specially a consideration in programming for libraries, such that if you mean to be kind to the library's users, you should write your library as lots of small object files, typically one non-static function per object, so that the user of the library doesn't bloat from having to carry code that actually never executes.

使用/Gy,编译器将创建具有COMDAT的目标文件.每个函数都在其自己的COMDAT中,该COMDAT在某种程度上是一个微型对象.如果链接程序只需要对象中的一个功能,则可以仅选择该功能.链接器的/OPT开关可​​让您对链接器以这种选择性进行的操作进行一些控制-但如果没有/Gy,则没有任何选择.

With /Gy, the compiler creates object files that have COMDATs. Each function is in its own COMDAT, which is to some extent a mini-object. If the linker wants just one function from the object, it can pick out just that one. The linker's /OPT switch gives you some control over what the linker does with this selectivity - but without /Gy there's nothing to select.

或者很少.至少可以想象,链接器可以折叠功能,这些功能每个都是目标文件中的全部代码,并且碰巧具有相同的代码.可以肯定的是,链接器可以消除整个对象文件,其中不包含任何未引用的对象.毕竟,它对库中的目标文件执行此操作.但是,实际上,过去的规则是,如果将非COMDAT对象文件添加到链接器的命令行中,则表示您希望在二进制文件中使用该文件,即使未引用也是如此.通常,可以想象的和完成的事情之间的差异是巨大的.

Or very little. It's at least conceivable that the linker could, for instance, fold functions that are each the whole of the code in an object file and happen to have identical code. It's certainly conceivable that the linker could eliminate a whole object file that contains nothing that's referenced. After all, it does this with object files in libraries. The rule in practice, however, used to be that if you add a non-COMDAT object file to the linker's command line, then you're saying you want that in the binary even if unreferenced. The difference between what's conceivable and what's done is typically huge.

那么,最好还是坚持快速回答.链接器选项受益于能够从每个目标文件内部分离函数(和变量),但是分离取决于要组织成COMDAT的代码和数据,这是编译器的工作.

Best, then, to stick with the quick answer. The linker options benefit from being able to separate functions (and variables) from inside each object file, but the separation depends on the code and data to have been organised into COMDATs, which is the compiler's work.

=================================

===================================

这篇关于MSVC编译器之间的关系COMDAT折叠的链接器选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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