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

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

    You can use the ".init*" sections to add C code to be run before main() (and even the C runtime). These sections are linked into the executable at the end and called up at specific time during program initialization. You can get the list here:

    http://www.nongnu.org/avr-libc/user-manual/mem_sections.html

    .init1 for example is weakly bound to __init(), so if you define __init(), it will be linked and called first thing. However, the stack hasn't been setup, so you have to be careful in what you do (only use register8_t variable, not call any functions).

提交回复
热议问题