Using D static library from C

George Sapkin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 5 11:51:23 PDT 2014


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.


More information about the Digitalmars-d-learn mailing list