What happens when you launch a D application ?

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 22 04:51:00 PDT 2015


On Friday, 22 May 2015 at 11:13:43 UTC, Suliman wrote:
> Am I right understand that that:
> 1. every App start from main()
> 2. Dmain is function that run after main is started and it's 
> run GC, unit-tests and so on?

Not really, it depends what you mean by main, the function called 
main that you write in your source code or the function called 
main that ends up in the actual program?

Here's an example:

int a;

static this()
{
   a = 4;
}

void main() //
{
   a = 5;
}

Conceptually speaking, the compiler turns this in to:

int a;

extern(C) int main()
{
     a = 4;
     return _Dmain();
}

int _Dmain()
{
     a = 5;
     return 0;
}

I think that in practice it's more like this:

int a;

void staticConstructor1()
{
     a = 4;
}

//actually in druntime
int _d_run_main(int function() sourceCodeMain)
{
     staticConstructor1();
     // and all the other necessary setup
     // for the program and runtime

     sourceCodeMain();
}

extern(C) int main()
{
     _d_run_main(&_Dmain);
}

int _Dmain
{
     a = 5;
     return 0;
}


More information about the Digitalmars-d-learn mailing list