我怎么能执行C / C ++ pre-主要初始化与AVR-GCC? [英] How can I perform pre-main initialization in C/C++ with avr-gcc?
问题描述
为了确保一些初始化code之前运行主
(使用的Arduino / AVR-GCC)我有code,如以下内容:
In order to ensure that some initialization code runs before main
(using Arduino/avr-gcc) I have code such as the following:
class Init {
public:
Init() { initialize(); }
};
Init init;
在理想情况下,我想能够简单地写:
Ideally I'd like to be able to simply write:
initialize();
但是,这并不编译...
but this doesn't compile...
有没有一种更简洁的方式来达到同样的效果?
Is there a less verbose way to achieve the same effect?
注意:的code是一个Arduino草图的一部分,所以在主
功能是自动生成的,不能被修改(例如调用初始化
之前的任何其他code)。
Note: the code is part of an Arduino sketch so the main
function is automatically generated and cannot be modified (for example to call initialize
before any other code).
更新:理想初始化将在设置执行
的功能,但在这种情况下,有其他的code取决于它这发生之前主
。
Update: ideally the initialization would be performed in the setup
function, but in this case there is other code depending on it which occurs before main
.
推荐答案
您可以使用GCC的<一个href=\"http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#index-g%5Ft%5F0040$c$c%5F007bconstructor%5F007d-function-attribute-2220\"><$c$c>constructor$c$c>属性,以确保它得到前所谓的main()
:
You can use GCC's constructor
attribute to ensure that it gets called before main()
:
void Init(void) __attribute__((constructor));
void Init(void) { /* code */ } // This will always run before main()
这篇关于我怎么能执行C / C ++ pre-主要初始化与AVR-GCC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!