可以在主输入之前立即注册要运行的功能吗?我知道所有全局对象都是在进入main之前创建的,所以我可以将代码放在全局对象的构造函数中,但这并不保证任何特定的顺序.我想做的是将一些注册码放入构造函数中,但是唉,我不知道该放什么:)我想这是高度系统特定的?
解决方法
如果您正在使用
gcc,则可以使用函数上的构造函数属性在main之前调用它(有关更多详细信息,请参阅
documentation).
@H_301_8@constructor
@H_301_8@destructor
The @H_301_8@constructor attribute causes the function to be called automatically before execution enters @H_301_8@main (). Similarly,the @H_301_8@destructor attribute causes the function to be called automatically after @H_301_8@main () has completed or @H_301_8@exit () has been called. Functions with these attributes are useful for initializing data that will be used implicitly during the execution of the program.