在main之前立即调用函数 [英] Calling a function immediately before main

查看:161
本文介绍了在main之前立即调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在输入 main 之前,是否可以立即注册要立即运行的函数 我知道所有的全局对象在输入 main 之前创建,所以我可以把代码放在一个全局对象的构造函数中,但不保证任何特定的顺序。我想要做的是把一些注册代码到构造函数,但唉,我不知道放什么:)我猜这是高度系统特定?

Is is possible to register a function to be run immediately before main is entered? I know that all global objects are created before entering main, so I could put the code in the constructor of a global object, but that does not guarantee any particular order. What I would like to do is put some registration code into the constructor, but alas, I don't know what to put there :) I guess this is highly system-specific?

推荐答案

如果您使用 gcc ,可以使用构造函数属性在 main 之前调用它(参见文档了解详情)。

If you're using gcc, you can use the constructor attribute on a function to have it called before main (see the documentation for more details).


code>

constructor

destructor

构造函数属性导致函数在执行进入 main()之前被自动调用。类似地, destructor 属性导致在 main()完成后或 exit()已被调用。具有这些属性的函数可用于初始化在程序执行期间隐式使用的数据。

The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () has completed or exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.

这篇关于在main之前立即调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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