C++ COMDAT 部分用于什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1834597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:20:24  来源:igfitidea点击:

What is the COMDAT section used for?

c++windowscoff

提问by Scott J

I see the /Gy option and am wondering why I would use it? http://msdn.microsoft.com/en-us/library/xsa71f43.aspx

我看到 /Gy 选项,想知道为什么要使用它?http://msdn.microsoft.com/en-us/library/xsa71f43.aspx

回答by Scott Wisniewski

The currently accepted answeris somewhat incomplete.

目前接受的答案是有些不完整。

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 部分的目的是允许在多个目标文件中定义“重复”部分。通常,如果在多个目标文件中定义了相同的符号,链接器将报告错误。这可能会导致某些 C++ 语言功能(例如模板)出现问题,这些功能可能会在不同的 cpp 文件中实例化相同的符号。

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.

COMDAT 部分用于解决此问题。当一个节在对象文件中被标记为 COMDAT 时,它还指定了一个标志,指示应如何解决冲突。有很多选项,包括“只选择你喜欢的任何人”、“确保所有副本的大小相同”、“确保所有副本的内容相同”、“选择最大的”等。参见完整列表的 COFF 规范。

In any case, unlike what the currently accepted answersaid, there's no requirements, 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.

在任何情况下,与当前接受的答案所说的不同,对于 COMDAT 部分的内容必须是什么,没有任何要求。它们可以包含一个过程、多个过程、数据或代码和数据的任意组合。

回答by Dmitriy Yurchenko

/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.

/Gy 选项非常适合用于发布版本。因为每个函数都有自己的部分,链接器可以删除每个未使用的代码段。如果您不指定 /Gy 选项,您将得到如下结果:“a.cpp”定义了 3 个函数,编译器在编译时将它们放在一个代码段中。“main.cpp”仅使用“a.cpp”中的一个函数,但是当链接“a.cpp”中的所有函数时,将链接(其中两个作为负担)。如果每个函数都有自己的部分,链接器只能选择一个包含“main.cpp”所需函数的部分。不使用 /Gy 选项进行编译有利于调试版本。编译一个类时,默认情况下,它的所有方法都有自己单独的部分。