Walter Bright wrote:
Tom S wrote:
I've tried it before and unfortunately there's a problem with this approach: static ctors from modules linked from a .lib don't get executed:

Not everything is linked in from a library, just referenced things (that's the whole point of a library). There is nothing referenced in mod.d. But rewriting as:

---------------
import mod;

void main()
{
    foo();
}
---------------
import std.c.stdio;

void foo() { }

static this() {
        printf("Running a static ctor for Mod"\n);
}
---------------

Compiling and running:

C:>dmd -lib mod

C:>dmd test5 mod.lib

C:>test5
Running a static ctor for Mod


Or, compile the module with just the static constructor in it separately and add it explicitly to the link list.

This one still has problems.

---- Main.d:
import Mod;

void main() {
        foo();
}
---- Mod.d:
import Mod2;

extern (C) int printf(char*, ...);

void foo() {
        foo2();
}

static this() {
        printf("Running a static ctor for Mod"\n);
}
---- Mod2.d:
extern (C) int printf(char*, ...);

void foo2() {
}

static this() {
        printf("Running a static ctor for Mod2"\n);
}
----

Compiling these modules as:
dmd -c Main.d
dmd -lib Mod.d Mod2.d
dmd Main.obj Mod.lib
... and running Main.exe yields:
Running a static ctor for Mod

So it seems that Main.d would have to reference symbols from each module in the program. Ouch.

Additionally, using such an approach breaks any possibility to use incremental compilation. So there's also the option of having a .lib per .d file and linking that all together. So I've just tried it:
dmd -c Main.d
dmd -lib Mod.d
dmd -lib Mod2.d
dmd Main.obj Mod.lib Mod2.d
... and OPTLINK crashed with the familiar MsgBox containing register values.

While we're at incremental compilation, this bug: http://d.puremagic.com/issues/show_bug.cgi?id=1629 often kills it for me and it's been around for a few whiles already.


--
Tomasz Stachowiak
http://h3.team0xf.com/
h3/h3r3tic on #D freenode

Reply via email to