6

Let's say I have a very basic class:

// lib.h
class A
{
public:
    A();
    void nop();
};

// lib.cpp
#include "lib.h"

A::A() {}
void A::nop() {}

, which I compile into a shared library:

g++ lib.cpp -shared -o lib.so -fPIC

When I look at the exported symbols from the code section of the library, I see three of them:

$ nm lib.so | grep ' T '
00000000000010f6 T _ZN1A3nopEv
00000000000010ea T _ZN1AC1Ev
00000000000010ea T _ZN1AC2Ev
$ nm lib.so | grep ' T ' | cut -d ' ' -f3 | xargs c++filt
A::nop()
A::A()
A::A()

Why is the constructor exported twice? I've tested on Linux with both gcc and clang and the result is the same.

P.S.: I'm not trying to solve a real problem, this is just a peculiarity I noticed.

Claudiu
  • 2,124
  • 4
  • 26
  • 36
  • 1
    Exciting question. Out of curiosity, I googled "gcc constructor name mangling" ... and found a duplicate. ;-) – Scheff's Cat Oct 22 '19 at 11:16
  • Concerning clang, I guess it just follows the same ABI. – Scheff's Cat Oct 22 '19 at 11:18
  • @Scheff Wow, you have really good searching skills :) What is interesting is that even if I declare the class as "final", I still get two constructors. – Claudiu Oct 22 '19 at 11:22
  • 1
    Yeah. If I understood it right that's covered as well in the dupe - _this is simply a by-product of polymorphism support, even though it's not actually required in this case._ – Scheff's Cat Oct 22 '19 at 11:24

0 Answers0