Dual Emission of Constructor Symbols
In C code compiled with GCC or clang, constructor definitions often appear twice in static libraries. This behavior raises questions about the reason for this duplication and its implications.
Cause of Dual Emission
According to the Itanium C ABI, constructor mangled names consist of a prefix, a nested class and function name, an end-nested tag, and parameter information. However, for constructors in classes without virtual base classes, GCC additionally emits two variants: "complete object constructor" (C1) and "base object constructor" (C2).
This dual emission stems from the ABI's support for polymorphism. Even if a class does not have virtual base classes, the ABI requires this dual emission to facilitate polymorphic handling.
Implications of Dual Emission
Despite emitting two constructor symbols, GCC aliases the symbols to the same code during compilation optimization. This means there are no multiple definition errors.
Additionally, the assembly code generated for both constructor symbols demonstrates that they call the same underlying constructor logic. Regardless of which symbol is called, the complete object constructor is invoked. This behavior implies that the C2 symbol is a vestige of the ABI's support for polymorphism, even in cases where it is not strictly necessary.
The above is the detailed content of Why Do C Constructor Symbols Appear Twice in Static Libraries?. For more information, please follow other related articles on the PHP Chinese website!