How can I perform pre-main initialization in C/C++ with avr-gcc?

后端 未结 9 2016
名媛妹妹
名媛妹妹 2020-12-28 09:21

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         


        
9条回答
  •  孤独总比滥情好
    2020-12-28 10:19

    If you are using the Arduino environment, is there any reason you can't place it in the setup method?

    Of course, this is after the Arduino-specific hardware setup, so if you have such low-level stuff that it really has to go before main, then you need some constructor magic.

    UPDATE:

    Ok, if it has to be done before the main I think the only way is to use a constructor like you already do.

    You can always make a preprocessor macro of it:

    #define RUN_EARLY(code) \
    namespace { \
        class Init { \
            Init() { code; } \
        }; \
        Init init; \
    }
    

    Now this should work:

    RUN_EARLY(initialize())
    

    But it's not really making things shorter, just moving the verbose code around.

提交回复
热议问题