I'm trying to link a simple D static library to C code, but I'm can't figure out how to do it properly without getting a segfault when running it.

test.d
-------------------------------
import std.stdio;

extern(C) void funcD() {
    writeln("From D");
}

main.c
-------------------------------
int main() {
    rt_init(0);
    funcD();
    rt_term(0);
    return 0;
}

What I would expect to work is:
-------------------------------
dmd -lib test.d -oflibtest.a
gcc main.c -ltest -L./ -otest

However this complains about undefined references rf_init, rf_term and so on.
So I've tried linking it to phobos2:
-------------------------------
dmd -lib test.d -oflibtest.a
gcc main.c -ltest -L./ -otest -lphobos2

That compiles, however I get a segfault from rt.sections_linux.checkModuleCollisions()

I can make this compile and run like this:
-------------------------------
dmd -c test.d
gcc main.c test.o -lphobos2 -otest

But this prevent's me from integrating it with some build tools that expect a static library instead of a bunch of object files.

Cursory googling did not provide an immediate answer how to make the first option work. I guess I'm missing something obvious.

I'm using DMD64 D Compiler v2.065 on Fedora 20 x64.

Reply via email to