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

后端 未结 9 1962
名媛妹妹
名媛妹妹 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:22

    There is how I perform pre-main coding. There are sever init sections executed before main, refers to http://www.nongnu.org/avr-libc/user-manual/mem_sections.html initN sections.

    Anyhow, this only works on -O0 optimization for some reason. I still try to find out which option "optimized" my pre-main assembly code away.

    static void
    __attribute__ ((naked))
    __attribute__ ((section (".init8")))    /* run this right before main */
    __attribute__ ((unused))    /* Kill the unused function warning */
    stack_init(void) {assembly stuff}
    

    Update, it turns out I claimed this function is unused, leading to optimize the routine away. I was intended to kill function unused warning. It is fixed to used used attribute instead.

提交回复
热议问题