When you compile a program using MSVC and Microsoft's CRT you'll notice it auto-magically figures out where your main function is, what the prototype is and calls into it.
This doesn't have much to do with calling conventions (as posted before in a "similar" question) since you can override the default calling convention AND on Windows x64 the only calling convention is __fastcall
.
With that being said, how does Microsoft's CRT implementation figure out which main function you've declared and then includes a header file with definitions such as _SCRT_STARTUP_MAIN
, _SCRT_STARTUP_WMAIN
, _SCRT_STARTUP_WINMAIN
, _SCRT_STARTUP_WWINMAIN
, etc etc?
For example, if you make a function like so and compile as an exe: int __cdecl main()
... the linker will somehow do the steps as follows:
Somehow figure out which function you're using, no matter the function prototype. As long as it's one of the valid
main
prototypes, it'll work regardless of the arguments or return types.Then it will somehow use the file
exe_main.cpp
which has a definition_SCRT_STARTUP_MAIN
defined right before includingexe_common.inl
.Finally, inside
exe_common.inl
there's a check for the macro_SCRT_STARTUP_MAIN
and will finally invoke the correct main function (once again, magically, without having issues with multiple definitions).
Is there any way whatsoever to mimic this behavior in my own project so I can provide any type of main function and auto-magically figure out what type of main function is being used?