预启动初始化 [英] pre startup initialization

查看:68
本文介绍了预启动初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在调用main函数之前调用函数?

函数需要从不同的位置多次调用。


我有这样的工作,但它更像是一个黑客...

--- FOO.cpp ---

bool func(){

dostuff;

返回0;

};


--- A.cpp ---

bool func();

bool __dummyvar _ = func();


--- B.cpp ---

bool func();

bool __dummyvar _ = func();


--- C.cpp ---

bool func();

bool __dummyvar _ = func();


全部全部完成。

How would I go about calling a function before the main function is called?
The function needs to be called multiple times from different locations.

I have something like this working but it is more of a hack than anything...
---FOO.cpp---
bool func() {
dostuff;
return 0;
};

---A.cpp---
bool func();
bool __dummyvar__ = func();

---B.cpp---
bool func();
bool __dummyvar__ = func();

---C.cpp---
bool func();
bool __dummyvar__ = func();

this is all done globally.

推荐答案

* Nolan Martin:
* Nolan Martin:
如何在调用main函数之前调用函数?
该函数需要从不同的地方多次打电话。

我有这样的工作,但它比任何东西更像是一个黑客...
--- FOO.cpp ---
bool func(){
dostuff;
返回0;
} ;

--- A.cpp ---
bool func();
bool __dummyvar__ = func();

--- B .cpp ---
bool func();
bool __dummyvar__ = func();

--- C.cpp ---
bool func() ;
bool __dummyvar _ = func();

这一切都是在全球范围内完成的。
How would I go about calling a function before the main function is called?
The function needs to be called multiple times from different locations.

I have something like this working but it is more of a hack than anything...
---FOO.cpp---
bool func() {
dostuff;
return 0;
};

---A.cpp---
bool func();
bool __dummyvar__ = func();

---B.cpp---
bool func();
bool __dummyvar__ = func();

---C.cpp---
bool func();
bool __dummyvar__ = func();

this is all done globally.




对不起我不喜欢没有一个非常好的,简单的解决方案。


我怀疑是因为它不能在任何地方被发现因为它需要语言支持
这在C ++中并不存在。


AFAIK标准的i / o流(包括C和C ++)初始化为

某些机制不可用于普通

非标准库实现程序员。


请注意,虽然命名空间的动态初始化顺序为

范围对象_within_翻译单元很好efined(即定义

的定义),翻译单元之间的初始化顺序非常好

很多未定义。


另请注意如果func()抛出它真的再见,那么或许更多

手册计划会更好 - 更多的工作,但更安全。


-

答:因为它弄乱了人们通常阅读文本的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet上最烦人的事情是什么?在电子邮件中?



I''m sorry that I don''t have a really good, simple solution.

I suspect it is because such isn''t to be found anywhere because it
requires language support that isn''t present in C++.

AFAIK the standard i/o streams (both C and C++) are initialized by
some mechanism not available to ordinary
non-standard-library-implementor programmers.

And note that although the order of dynamic initialization of namespace
scope objects _within_ a translation unit is well-defined (namely order
of definition), order of initialization among translation units is very
much undefined.

Also note that if func() throws it''s really goodbye, so perhaps a more
"manual" scheme would be better -- more work, but safer.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Nolan Martin发布:
Nolan Martin posted:
我如何在主$ b $之前调用函数b函数被称为?该函数需要从不同位置多次调用


我有类似这样的工作,但它比任何东西更多的是
hack ... --- FOO .cpp ---
bool func(){
dostuff;
返回0;
};

--- A.cpp ---
bool func();
bool __dummyvar__ = func();

--- B.cpp ---
bool func();
bool __dummyvar__ = func();

--- C.cpp ---
bool func();
bool __dummyvar _ = func();
How would I go about calling a function before the main function is called? The function needs to be called multiple times from different locations.

I have something like this working but it is more of a hack than anything... ---FOO.cpp---
bool func() {
dostuff;
return 0;
};

---A.cpp---
bool func();
bool __dummyvar__ = func();

---B.cpp---
bool func();
bool __dummyvar__ = func();

---C.cpp---
bool func();
bool __dummyvar__ = func();

this is all done globally.



这很聪明!我不会想到这一点,我会用
定义一个全局对象,它的构造函数就是这样。


无论如何,C ++程序从main(),周围没有得到

,尽管...在调用main之前,所有全局数据都已初始化

。同时,如果你想在main()之前执行

代码,这个代码必须作为初始化全局变量的结果执行,没有两个

关于它的方式。


所以你所做的就是完美。但是......你的代码包含

同一个对象的三个定义。我们称之为违反ODR,The One Definiton Rule的




在以下代码中:
< br $>
--- FOO.cpp ---

bool func()

{

// Do Stuff


返回true;

}


--- A.cpp ---

bool func();


bool g_init1 = func();


--- B.cpp ---

bool func();


bool g_init2 = func();


--- C.cpp ---

bool func();


bool g_init3 = func();


--- main.cpp ---

int main()

{

;

}

当你执行这个程序时,这就是'会发生什么':


func();

func();

func();

main();

g_init1,g_init2和g_init3将在no

特定订单中初始化。


我不会称你所做的是黑客,这是非常的简单

发生什么事情并且运作良好!

-JKop


That''s clever! I wouldn''t have thought of that, I would''ve
defined a global object whose contructor does stuff.

Anyway, C++ programs start at main(), there''s no getting
around that, although... all global data is initialized
before main is called. Concordantly, if you want to execute
code before main(), this code must be executed as a
consequence of a global variable being initialized, no two
ways about it.

So what you''ve done is perfect. But... your code contains
three definitions of the same object. That''s we call a
violation of the "ODR", The One Definiton Rule.

In the following code:

---FOO.cpp---
bool func()
{
//Do Stuff

return true;
}

---A.cpp---
bool func();

bool g_init1 = func();

---B.cpp---
bool func();

bool g_init2 = func();

---C.cpp---
bool func();

bool g_init3 = func();

---main.cpp---
int main()
{
;
}
When you execute this program, here''s what''ll happen:

func();
func();
func();
main();

g_init1, g_init2 and g_init3 will be initialized in no
particular order.

I wouldn''t call what you''ve done a "hack", it''s very simple
what going on and it works well!
-JKop


Nolan Martin写道:
Nolan Martin wrote:
如何在调用main函数之前调用函数?
该函数需要从不同的位置多次调用。

>我有类似的工作,但它更像是一个黑客攻击...
--- FOO.cpp ---
bool func(){
dostuff; 返回0;
};

--- A.cpp ---
bool func();
bool __dummyvar__ = func();

--- B.cpp ---
bool func();
bool __dummyvar__ = func();

--- C.cpp-- -
bool func();
bool __dummyvar _ = func();

全部全部完成。
How would I go about calling a function before the main function is called?
The function needs to be called multiple times from different locations.

I have something like this working but it is more of a hack than anything...
---FOO.cpp---
bool func() {
dostuff;
return 0;
};

---A.cpp---
bool func();
bool __dummyvar__ = func();

---B.cpp---
bool func();
bool __dummyvar__ = func();

---C.cpp---
bool func();
bool __dummyvar__ = func();

this is all done globally.



另一种策略是将当前功能在

main()中移动到辅助部分。功能。 main()函数

会调用你的pre-initialize函数然后调用

辅助主函数。


只是想一想,因为你还没有再给我们了

具体细节。


在嵌入式系统领域,必须在main()组装完成之前初始化的东西是
语言。


-

托马斯马修斯


C ++新闻组欢迎辞:
http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http://www.parashift。 com / c ++ - faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c -c ++ faq:
http:// www。 raos.demon.uk/acllc-c ++ /的faq.html <其他网站:
http:// www。 josuttis.com - C ++ STL图书馆书



Another tactic is to move your current functionality in
main() to a "secondary" function. The main() function
would call your "pre-initialize" functions then call the
secondary main function.

Just a thought, since you haven''t given us any more
specifics.

In the embedded systems world, things that must be
initialized before main() are done in assembly
language.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


这篇关于预启动初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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