Avoiding the main (entry point) in a C program

前端 未结 7 2096
迷失自我
迷失自我 2020-12-01 10:50

Is it possible to avoid the entry point (main) in a C program. In the below code, is it possible to invoke the func() call without calling via main()

7条回答
  •  感动是毒
    2020-12-01 11:38

    If you are using an open source compiler such as GCC or a compiler targeted at embedded systems you can modify the C runtime startup (CRT) to start at any entry point you need. In GCC this code is in crt0.s. Generally this code is partially or wholly in assembler, for most embedded systems compilers example or default start-up code will be provided.

    However a simpler approach is to simply 'hide' main() in a static library that you link to your code. If that implementation of main() looks like:

    int main(void)
    {
        func() ;
    }
    

    Then it will look to all intents and purposes as if the user entry point is func(). This is how many application frameworks with entry points other than main() work. Note that because it is in a static library, any user definition of main() will override that static library version.

提交回复
热议问题