使用 init_seg 调用初始化函数 [英] Using init_seg to call an initialization function
问题描述
我试图确保程序首先调用一个函数,以确保关键对象的初始化.但是,我对 init_seg
应该如何被使用/调用感到困惑.
I am trying to ensure a function will get called first by the program to ensure the initizilation of critical objects. However, I am confused as to how init_seg
is suppose to be getting used/called.
例如,从 msdn 的文档看来,以下内容似乎是第一个被调用的.
For example, from the documentation at msdn it would seem as if the following would be the first to be called.
#pragma init_seg( ".CRT$XCC" )
static void InitializeStuff()
{
printf("InitializeStuff\n");
}
然而,情况似乎并非如此,因为该函数显然不会被自动调用(当然,除非我明确调用它).
However, that does not seem to be the case as that function will just plainly not get called automatically (unless I call it explicitly, of course).
我如何使用 init_set 来确保在我的其他执行代码的其余部分之前调用一个函数?
How can I use init_set to ensure a function is called before the rest of my other executing code?
推荐答案
如果我理解正确,您将所有要首先初始化的全局变量与 #pragma init_seg
If i have understood it right you place all global variables that you want to be initialized first in the same file as the #pragma init_seg
示例:
#pragma init_seg( ".CRT$XCB" )
class A
{
A()
{
printf("first!\n");
}
};
static A first;
并确保除了要首先初始化的全局变量之外,不要在该文件中放置任何其他全局变量.
And make sure you dont place any other globals in that file than the ones you want to initialized first.
这篇关于使用 init_seg 调用初始化函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!