开发者

Why does g++ generate multiple (weak) similar symbols?

开发者 https://www.devze.com 2022-12-19 02:50 出处:网络
I\'m looking at the output of nm -C 0804a86a W ForkMessageHandler::ForkMessageHandler() 0804a86a W ForkMessageHandler::ForkMessageHandler()

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 Multi开发者_JS百科MessageHandler::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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消