What happens when you launch a D application ?

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat May 23 02:01:02 PDT 2015


On 5/22/2015 10:26 PM, Suliman wrote:
> Really hard to understand...
>
> So what what would call at first ?
> extern(C) int main()
> or
> int _Dmain()

Your D programs have multiple layers. There is the C runtime, DRuntime, 
and your program code.

The C runtime is at the bottom. When the program launches, it gets 
control first. When it has done its work, it looks for a main function 
and calls it. Normally, in a C program, that would be the main function 
that you implement. But in D, it is a function implemented by DRuntime.

The DRuntime main, because it is called by C, must be declared as 
extern(C) in order for the C runtime to recognize it. When it is called 
by the C runtime, then DRuntime does some housekeeping work (calling 
module constructors, intializing the garbage collecter, and so on). When 
it is done with that, it then calls the main function that you 
implemented in your program.

The exception to this is when a program is compiled on Windows with 
WinMain enabled. In this case, the C main is not the entry point, but 
WinMain is instead. DRuntime does not implement WinMain, so when 
compiling a D program like this, we have to call initialize DRuntime 
manually.


More information about the Digitalmars-d-learn mailing list