Module static constructor doesn't work?

Andrey Zherikov andrey.zherikov at gmail.com
Thu Aug 8 14:55:37 UTC 2019


I have the following code:

// lib1/lib.d
module lib;

import std.stdio;

static this()
{
     writeln("+" ~ __FILE__);
}

static ~this()
{
     writeln("-" ~ __FILE__);
}

// main.d
int main()
{
     import std.stdio;
     writeln("hello");
     return 0;
}

So if I compile lib.d and main.d together then ctor/dtor are 
called:
$ dmd.exe main.d lib1/lib.d && main.exe
+lib1\lib.d
hello
-lib1\lib.d

But if I create library from lib.d first and then link it with 
main.d then ctor/dtor are not called:
$ dmd.exe -lib lib1/lib.d -od=lib1
$ dmd.exe main.d lib1/lib.lib && main.exe
hello


More information about the Digitalmars-d-learn mailing list