C ++:什么时候(以及如何)是C ++全局静态构造函数? [英] C++: When (and how) are C++ Global Static Constructors Called?

查看:160
本文介绍了C ++:什么时候(以及如何)是C ++全局静态构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一些C ++代码,我遇到了一个问题,一直困扰我一段时间...假设我编译的GCC在Linux主机上的ELF目标,其中是全局静态构造函数和析构函数叫做?



我听说在crtbegin.o中有一个函数_init,在crtend.o中有一个函数_fini。这些是由crt0.o调用吗?或者动态链接器是否实际检测到它们在加载的二进制文件中的存在并调用它们?



我主要感兴趣的是知道所以我可以理解在幕后发生的事情,我的



更新:我已经在运行时加载,执行并卸载代码。

m基本上试图找出构造函数被调用的一般时间。我不想在我的代码基于这些信息做假设,它或多或少,以获得更好地了解当我的程序加载时在较低级别发生了什么。

解决方案

当谈到这个问题时,关于非本地静态对象没有很多保证。正如你已经知道的(它也在这里提到),它不应该编写依赖于它的代码。静态初始化顺序fiasco ...



静态对象通过两阶段初始化:静态初始化和动态初始化。前者首先发生,并通过常量表达式执行零初始化或初始化。后者发生在所有静态初始化完成后。这是调用构造函数时的例子。



一般来说,这个初始化发生在main()之前的某个时候。然而,相对于许多人认为,即使这不是由C ++标准保证。实际上保证的是,在使用与被初始化的对象相同的翻译单元中定义的任何函数或对象之前完成初始化。请注意,这不是操作系统特定的。这是C ++规则。这是标准的一个引用:


它是实现定义的,无论动态初始化(8.5,9.4,12.1,12.6.1)
命名空间范围的对象在main的第一个语句之前完成。如果初始化在main的第一个语句之后在时间上延迟到某个点
,它将在首次使用任何函数或对象定义
之前发生在与要初始化的对象相同的翻译单元


I'm working on some C++ code and I've run into a question which has been nagging me for a while... Assuming I'm compiling with GCC on a Linux host for an ELF target, where are global static constructors and destructors called?

I've heard there's a function _init in crtbegin.o, and a function _fini in crtend.o. Are these called by crt0.o? Or does the dynamic linker actually detect their presence in the loaded binary and call them? If so, when does it actually call them?

I'm mainly interested to know so I can understand what's happening behind the scenes as my code is loaded, executed, and then unloaded at runtime.

Thanks in advance!

Update: I'm basically trying to figure out the general time at which the constructors are called. I don't want to make assumptions in my code based on this information, it's more or less to get a better understanding of what's happening at the lower levels when my program loads. I understand this is quite OS-specific, but I have tried to narrow it down a little in this question.

解决方案

When talking about non-local static objects there are not many guarantees. As you already know (and it's also been mentioned here), it should not write code that depends on that. The static initialization order fiasco...

Static objects goes through a two-phase initialization: static initialization and dynamic initialization. The former happens first and performs zero-initialization or initialization by constant expressions. The latter happens after all static initialization is done. This is when constructors are called, for example.

In general, this initialization happens at some time before main(). However, as opposed to what many people think even that is not guaranteed by the C++ standard. What is in fact guaranteed is that the initialization is done before the use of any function or object defined in the same translation unit as the object being initialized. Notice that this is not OS specific. This is C++ rules. Here's a quote from the Standard:

It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of namespace scope is done before the first statement of main. If the initialization is deferred to some point in time after the first statement of main, it shall occur before the first use of any function or object defined in the same translation unit as the object to be initialized

这篇关于C ++:什么时候(以及如何)是C ++全局静态构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆