In a C/C++ program how does the system (windows, linux, mac OS X) call the main() function

前端 未结 7 1504
半阙折子戏
半阙折子戏 2020-12-09 03:51

I am looking for a more technical explanation then the OS calls the function. Can anyone help me out or point me to a website or book?

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 04:43

    main() is part of the C library and is not a system function. I don't know for OS X or Linux, but Windows usually starts a program with WinMainCRTStartup(). This symbol init your process, extract command line arguments and environment (argc, argv, end) and calls main(). It is also responsible of calling any code that should run after main(), like atexit().

    By looking in your Visual Studio file, you should be able to find the default implementation of WinMainCRTStartup to see what it does.

    You can also define a function of your own to call at startup, this is done by changing "entry point" in the linker options. This is often a function that takes no arguments and returns a void.

提交回复
热议问题