What is the difference between main and mainCRTStartup?

I am trying to understand how substituting another entry point for WinMain works in the Microsoft toolchain.

I already found this question , and it was very helpful, but one last detail grumbles at me.

The first time I changed the Linker>Advanced>Entry Point parameter in Visual Studio, I set it to main by mistake, and my program compiled and went fine. I realized this later and rebuilt the program by installing it on mainCRTStartup , as the accepted answer suggests in a related question, and did not find anything else.

So my question is: is there any difference between main and mainCRTStartup , and if so, what is the difference?

+6
source share
1 answer

main () is the entry point of your C or C ++ program. mainCRTStartup () is the entry point of the C runtime library. It initializes the CRT, calls any static initializers that you wrote in your code, and then calls your main () function.

Obviously, you must first perform both a CRT and your own initialization. You may suffer from it rather difficult to diagnose errors if this does not happen. Maybe you won’t do it, that shit. Something you can verify by pasting this code into a small C ++ program:

 class Foo { public: Foo() { std::cout << "init done" << std::endl; } } TestInit; 

If you change the entry point to "main", you will see that the constructor is never called.

This is bad.

+16
source

All Articles