tags:

views:

81

answers:

2

I'm looking at the output of nm -C

0804a86a W ForkMessageHandler::ForkMessageHandler()
0804a86a W ForkMessageHandler::ForkMessageHandler()
0804a6fa T ForkMessageHandler::~ForkMessageHandler()
0804a698 T ForkMessageHandler::~ForkMessageHandler()
0804a698 T ForkMessageHandler::~ForkMessageHandler()
0804a800 W MultiMessageHandler::MultiMessageHandler()
0804a800 W MultiMessageHandler::MultiMessageHandler()
0804a84c W MultiMessageHandler::~MultiMessageHandler()
0804a81c W MultiMessageHandler::~MultiMessageHandler()
0804a81c W MultiMessageHandler::~MultiMessageHandler()

Why does g++ generate many similar symbols, and what's the purpose of weak symbols ?

Edit: this is from the final executable, not a .o file. ForkMessageHandler is defined in a .cpp file, noone includes a header for it - but several includes headers for its base class.

A: 

My understanding is that GCC generates inline functions using weak symbols so for any calls to the function that aren't inlined it can link it to any one of the definitions and throw away the rest.

Michael Burr
But why the multiple definitions - wouldn't just one suffice ?
leeeroy
I don't really know, since I'm not particularly an expert on object file formats. One might well suffice, but maybe no one's bothered to worry about it since it'll get reduced down to at most one when everything is finished linking. Then again, there might be a more important reason that I'm not thinking of.
Michael Burr
+3  A: 

Those are the default constructors and automatically generated destructors. They will be generated as weak symbols in every compilation unit that includes the class definition to guarantee that there is at least one available.

The reason they are weak is to avoid conflicts in the linking process since the class definition will be present in every object file including the header file it's defined.

Laserallan