Home > Backend Development > C++ > body text

Why do constructors for classes without virtual base classes appear duplicated in GCC-compiled code?

Susan Sarandon
Release: 2024-11-16 16:01:03
Original
170 people have browsed it

Why do constructors for classes without virtual base classes appear duplicated in GCC-compiled code?

Dual Emission of Constructor Symbols

Understanding the Issue

In GCC-compiled code, constructors for classes without virtual base classes may appear duplicated in library listings, raising questions about the reasons behind this behavior. To elaborate, the Itanium C ABI defines a set of naming conventions for C symbols, including constructors.

ABI Parsing

Let's examine the mangled names of these constructors:

  • _Z3fooEv: Constructor for Thing::foo()
  • _ZN5ThingC1Ei: Complete object constructor for Thing(int)
  • _ZN5ThingC2Ei: Base object constructor for Thing(int)
  • _ZN5ThingC1Ev: Complete object constructor for Thing()
  • _ZN5ThingC2Ev: Base object constructor for Thing()

As you can see, the constructors are distinguished by the C1 (complete object constructor) and C2 (base object constructor) suffixes.

The Different Constructors

This duality stems from polymorphism support, although it is not strictly required in this case. The complete object constructor additionally initializes virtual base classes, while the base object constructor initializes data members and non-virtual base classes.

Multiple Definitions

Despite multiple entries in library listings, these duplicate constructors do not cause "multiple definition of symbol __" errors. This is because the ABI provides unique mangled names for each constructor type, ensuring that they are treated as distinct symbols by the linker.

Conclusion

The dual emission of constructor symbols in GCC-compiled code is a result of polymorphism support and the Itanium C ABI naming conventions. Understanding these concepts illuminates the reasons behind this behavior and addresses any concerns about multiple definitions.

The above is the detailed content of Why do constructors for classes without virtual base classes appear duplicated in GCC-compiled code?. 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