This covers Windows programming with 32 bit MSCOFF,64 bit MSCOFF, console programs, GUI programs, and DLLs.
A D console program is specified by having a function with D linkage at module level calledmain. When the compiler sees this, it triggers the mixin templatecore.internal.entrypoint._d_cmain to be added. The_d_cmain template looks like:
template _d_cmain(){extern(C) {int _d_run_main(int argc,char **argv,void* mainFunc);int _Dmain(char[][] args);int main(int argc,char **argv) {return _d_run_main(argc, argv, &_Dmain); }// Solaris, for unknown reasons, requires both a main() and an _main()version (Solaris) {int _main(int argc,char** argv) {return main(argc, argv); } } }}
Themain function in it, because it is marked asextern(C), is the entry point of a C console program. The executable starts up as a C executable, with the C runtime library initialization and then the C main() function is called.
The C runtime library runs any D functions tagged withpragma(crt_constructor) as part of its initialization, and before it calls Cmain(). The order in which these are run is not specified.
The Cmain function then calls the D runtime library function_d_run_main, passing itargc andargv, and the address of_Dmain. The compiler renames the Dmain function in the source code to_Dmain.
_d_run_main then initializes the D runtime library, convertsargc andargv toargs, and calls the Dmain function.
When the Dmain function returns, control is back in_d_run_main which shuts down the D runtime library, and then returns to the Cmain, which then returns to the C library which shuts down the C runtime library, then exits the program.
The C runtime library runs any D functions tagged withpragma(crt_destructor) as part of its shutdown, in the reverse order that thepragma(crt_constructor)s were run.
32 bit and 64 bit MSCOFF programs use the Microsoft Visual C/C++ compiler as theAssociated C Compiler, generate object files in the MSCOFF format, and use the Microsoft linker to link them.