Post by vl106I got the idea what is behind the linkage on function level. So I am just
curious what COMDAT stands for. COMmon DATa is my guess. But
isn't it mostly used for functions?
If you talk about /Gy (aka function-level linking) this is one of the cases
where the compiler emits data in COMDAT sections.
But COMDATs (or COMDAT groups) are required for C++ linkage in
several contexts:
- (referenced) inline functions (multiple definitions in different
translation units do not violate the ODR)
- Virtual Tables (VTT & VFTs) for a class need to be emitted
somewhere.
Some implementation use decider functions, but in some cases the
constructors/destructors and tables need to be emitted for every
translation unit. Consequently multiple definitions must not result
in a linker error.
- all things associated with the two above (virtual functions referenced,
local static variables, local static initialization guards, initializers,
nontrivial literals (although many compilers fail to get that right))
- (VC++ specific) __declspec(selectany) is placed in a COMDAT.
/Gy is for eliminating unused functions, which is only an optimization
and not required to make things work at all. /Gy also generates
COMDATs which do not allow duplicate definitions.
So COMDATs are not necessarily used for funcitons only,
but there is no corresponding optimization for /Gy for data (and
I have actually asked for that once but MS have no plans to do
that).
And yes, if I were to design linkage model C++ today, I would
do things quite differently ...
-hg