Home > Backend Development > C++ > Why Do C Constructor Symbols Appear Twice in Static Libraries?

Why Do C Constructor Symbols Appear Twice in Static Libraries?

Susan Sarandon
Release: 2024-11-27 22:33:11
Original
843 people have browsed it

Why Do C   Constructor Symbols Appear Twice in Static Libraries?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template