C++ g++ __static_initialization_and_destruction_0(int, int) - 它是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2434505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:28:25  来源:igfitidea点击:

g++ __static_initialization_and_destruction_0(int, int) - what is it

c++constructorg++destructor

提问by osgx

After compiling of c++ file (with global static object) I get in nmoutput this function:

编译 c++ 文件(带有全局静态对象)后,我得到了nm这个函数的输出:

 00000000 t _Z41__static_initialization_and_destruction_0ii

 __static_initialization_and_destruction_0(int, int)  /* after c++filt */

What is it? It will call __cxa_atexit()

它是什么?它会调用__cxa_atexit()

Can I disable generation of this function (and calling a __cxa_atexit()) and put all constructor and destructor calls to .ctorsand .dtorssections?

我可以禁用此函数的生成(并调用 a __cxa_atexit())并将所有构造函数和析构函数调用放到.ctors.dtors部分吗?

采纳答案by Xepo

This doc file seems to tell ya all you'd wanna know about those functions: http://www.nsnam.org/docs/linker-problems.doc

这个文档文件似乎告诉你所有你想知道的关于这些功能的信息:http: //www.nsnam.org/docs/linker-problems.doc

From what I can grok, gcc creates a __static_initialization_and_destruction_0for every translation unit that needs static constructors to be called. Then it places __do_global_ctors_auxinto the .ctorssection, which then calls __static_initialization_and_destruction_0on each translation unit.

据我所知,gcc__static_initialization_and_destruction_0为每个需要调用静态构造函数的翻译单元创建了一个。然后它__do_global_ctors_aux放入.ctors部分,然后调用__static_initialization_and_destruction_0每个翻译单元。

The issue seems to be a lot more complex than that though; gcc has to deal with individual object files in an archive, and I think this is how they keep the linker from optimizing out those calls.

不过,这个问题似乎要复杂得多;gcc 必须处理存档中的单个目标文件,我认为这就是它们防止链接器优化这些调用的方式。