COMDAT 部分的用途是什么?
我看到 /Gy 选项,想知道为什么要使用它? http://msdn.microsoft.com/en-us/library/xsa71f43.aspx
I see the /Gy option and am wondering why I would use it? http://msdn.microsoft.com/en-us/library/xsa71f43.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
COMDAT 节的目的是允许在多个目标文件中定义“重复”节。通常,如果在多个目标文件中定义了相同的符号,链接器将报告错误。这可能会导致某些 C++ 语言功能(例如模板)出现问题,这些功能可能会在不同的 cpp 文件中实例化相同的符号。
COMDAT 部分用于解决这个问题。当目标文件中的某个节被标记为 COMDAT 时,它还会指定一个标志,指示应如何解决冲突。有很多选项,包括“只选择您喜欢的任何人”,“确保所有重复项大小相同”,“确保所有重复项具有相同内容”,“选择最大的一个”等。请参阅完整列表的 COFF 规范。
无论如何,与其他答案所说的不同,对于 COMDAT 部分的内容必须是什么,没有任何要求。它们可以包含一个过程、多个过程、数据或代码和数据的任意组合。
The purpose of a COMDAT section is to allow "duplicate" sections to be defined in multiple object files. Normally, if the same symbol is defined in multiple object files, the linker will report errors. This can cause problems for some C++ language features, like templates, that may instantiate the same symbols in different cpp files.
COMDAT sections are used to get around this. When a section is marked as a COMDAT in an object file, it also specifies a flag that indicates how conflicts should be resolved. There are a bunch of options, including "just pick anyone you like", "make sure all dups. are the same size", "make sure all dups. have the same content", "pick the largest one", etc. See the COFF spec for a complete list.
In any case, unlike what other answers said, there's no requirement, one way or the other, on what the contents of a COMDAT section has to be. They can contain one procedure, many procedures, data, or any combination of both code and data.
/Gy 选项很适合在发布版本中使用。因为每个函数都有自己的部分,链接器可以删除所有未使用的代码。
如果您不指定 /Gy 选项,您将得到如下内容:
“a.cpp”定义了3个函数,编译器在编译时将它们放在一个代码段中。
“main.cpp”仅使用“a.cpp”中的一个函数,但在链接“a.cpp”中的所有函数时都将被链接(其中两个作为负担)。如果每个函数都有自己的部分,则链接器只能选取包含“main.cpp”所需函数的部分。
不使用 /Gy 选项进行编译有利于调试版本。
编译类时,默认情况下它的所有方法都有自己单独的部分。
/Gy option is good to be used in release builds. 'cause every function has it's own section, linker can drop every unused piece of code.
If you do not specify /Gy option you will get something like this:
"a.cpp" defines 3 functions and compiler puts them in one code section when compiling.
"main.cpp" uses only one function from "a.cpp", but when linking all of functions from "a.cpp" will be linked (two of them as a burden). And if every function had it's own section, linker could pick up only one that contained function needed by "main.cpp".
Compiling without /Gy option is good for debug builds.
When compiling a class, all of it's methods get their own separate sections by default.