5

Test case as follows:

// test.cpp
class X {
public:
    X();
};

X::X() { }

void foo() {
  X x;
}

Compile it and read the symbols in the object file like this:

[root@localhost tmp]# g++ -c test.cpp

[root@localhost tmp]# readelf -s -W test.o

Symbol table '.symtab' contains 12 entries:

Num: Value Size Type Bind Vis Ndx Name

 0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
 1: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS test.cpp
 2: 0000000000000000     0 SECTION LOCAL  DEFAULT    1 
 3: 0000000000000000     0 SECTION LOCAL  DEFAULT    3 
 4: 0000000000000000     0 SECTION LOCAL  DEFAULT    4 
 5: 0000000000000000     0 SECTION LOCAL  DEFAULT    6 
 6: 0000000000000000     0 SECTION LOCAL  DEFAULT    7 
 7: 0000000000000000     0 SECTION LOCAL  DEFAULT    5 
 8: 0000000000000000    10 FUNC    GLOBAL DEFAULT    1 _ZN1XC2Ev   => X::X()
 9: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT  UND __gxx_personality_v0
10: 0000000000000000    10 FUNC    GLOBAL DEFAULT    1 _ZN1XC1Ev   => X::X()
11: 000000000000000a    22 FUNC    GLOBAL DEFAULT    1 _Z3foov

[root@localhost tmp]# c++filt _ZN1XC1Ev

X::X()

[root@localhost tmp]# c++filt _ZN1XC2Ev

X::X()

Why does g++ generate two constructors with different name manglings(_ZN1XC1Ev and _ZN1XC2Ev)?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
ZhangXiongpang
  • 280
  • 2
  • 7
  • 1
    Some compilers generate two constructors, one to call from user programs and one to call when used as a base class of another object as there are some differences sometimes in what needs to be done. I'ma bit vague on the details and don't know if it's even the case here so this may be entirely wrong, and hence it's a comment not an answer :) – jcoder Sep 03 '13 at 09:00

1 Answers1

0

It is a known defect from G++. Refer Known g++ bugs

     G++ emits two copies of constructors and destructors.
     In general there are three types of constructors (and destructors).

     1.The complete object constructor/destructor.
     2.The base object constructor/destructor.
     3.The allocating constructor/deallocating destructor.

     The first two are different, when virtual base classes are involved. 

If you want to know more about this three types of ctors and dtors, refer link

Community
  • 1
  • 1
Saravanan
  • 1,270
  • 1
  • 8
  • 15