5

Take the following source code:

struct Foo {
    Foo(){}
};

Foo f;

When compiled with clang++, it creates a single symbol for the constructor:

clang++ -c foo.cpp
nm -C foo.o | grep Foo
0000000000000000 W Foo::Foo()

However, when compiled with g++, it creates multiple symbols for the constructor:

g++ -c foo.cpp;
nm -C foo.o | grep Foo
0000000000000000 W Foo::Foo()
0000000000000000 W Foo::Foo()
0000000000000000 n Foo::Foo()

Why would g++ create duplicate weak symbols in the same object file?
My only theory, is that it has to do with inlining, but that's just a guess.


I should note, the mangled names appear as so:

g++ -c foo.cpp; nm foo.o | grep Foo 
0000000000000000 W _ZN3FooC1Ev
0000000000000000 W _ZN3FooC2Ev
0000000000000000 n _ZN3FooC5Ev

So although the unmangled names are the same, ZN3FooC1Ev and ZN3FooC2Ev are different.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271

0 Answers0