www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using D static library from C

reply "George Sapkin" <george.sapkin gmail.com> writes:
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.
Jun 05 2014
next sibling parent reply "Stefan Koch" <Uplink.Coder gmail.com> writes:
You need to link the druntime too, I think.
Jun 05 2014
parent reply "George Sapkin" <george.sapkin gmail.com> writes:
On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote:
 You need to link the druntime too, I think.
At which stage? Can you provide a full command? Thanks.
Jun 05 2014
parent reply "Stefan Koch" <uplink.coder gmail.com> writes:
On Thursday, 5 June 2014 at 19:00:24 UTC, George Sapkin wrote:
 On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote:
 You need to link the druntime too, I think.
At which stage? Can you provide a full command? Thanks.
Err ... I just remeberd linking d with gcc needs a few libs -lpthread -lc -lm and possiby more though I use gdc I don't know what dmd links by default try to make an object file with gcc and link with dmd
Jun 05 2014
next sibling parent "George Sapkin" <george.sapkin gmail.com> writes:
On Thursday, 5 June 2014 at 19:14:34 UTC, Stefan Koch wrote:
 On Thursday, 5 June 2014 at 19:00:24 UTC, George Sapkin wrote:
 On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote:
 You need to link the druntime too, I think.
At which stage? Can you provide a full command? Thanks.
Err ... I just remeberd linking d with gcc needs a few libs -lpthread -lc -lm and possiby more though I use gdc I don't know what dmd links by default try to make an object file with gcc and link with dmd
I can link a D object (not a static lib) with gcc using -lphobos2 and that works. So additional libs does not solve anything. The trouble is linking a static lib without causing a segfault.
Jun 05 2014
prev sibling parent reply "George Sapkin" <george.sapkin gmail.com> writes:
On Thursday, 5 June 2014 at 19:14:34 UTC, Stefan Koch wrote:
 On Thursday, 5 June 2014 at 19:00:24 UTC, George Sapkin wrote:
 On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote:
 You need to link the druntime too, I think.
At which stage? Can you provide a full command? Thanks.
Err ... I just remeberd linking d with gcc needs a few libs -lpthread -lc -lm and possiby more though I use gdc I don't know what dmd links by default try to make an object file with gcc and link with dmd
I can link a D object (not a static lib) with gcc using -lphobos2 and that works. So additional libs does not solve anything. The trouble is linking a static lib without causing a segfault.
Jun 05 2014
parent reply "Mark Isaacson" <turck11 hotmail.com> writes:
I found that if I told dmd rather than gcc to do the linking that
everything just magically worked.

In other words:
gcc -c cstuff.c
dmd -c dstuff.d
dmd cstuff.o dstuff.o

I presume that dmd would be similarly smart with static libraries.
Jun 05 2014
parent "George Sapkin" <george.sapkin gmail.com> writes:
On Friday, 6 June 2014 at 02:01:12 UTC, Mark Isaacson wrote:
 I found that if I told dmd rather than gcc to do the linking 
 that
 everything just magically worked.

 In other words:
 gcc -c cstuff.c
 dmd -c dstuff.d
 dmd cstuff.o dstuff.o

 I presume that dmd would be similarly smart with static 
 libraries.
Yes, but I need to integrate this with gcc project. To get around phobos2 being linked twice and still have a static lib suitable for gcc I'm trying this now: dmd -c test.d ar r libtest.a test.o gcc main.c -ltest -L. -lphobos2 -otest It works with this small test project. I'll try to integrate this into bigger project now.
Jun 05 2014
prev sibling next sibling parent reply "Dave Wilson" <earthmandave gmail.com> writes:
You can create a static library from one or more .o files using 
ar, if that helps (unless I've failed to understand the 
question).  "ar r libtest.a test.o" should do the job.
Jun 05 2014
parent "George Sapkin" <george.sapkin gmail.com> writes:
On Thursday, 5 June 2014 at 19:10:17 UTC, Dave Wilson wrote:
 You can create a static library from one or more .o files using 
 ar, if that helps (unless I've failed to understand the 
 question).  "ar r libtest.a test.o" should do the job.
I don't have trouble creating a static library with dmd, I have trouble figuring out how to link it with C using gcc.
Jun 05 2014
prev sibling parent reply "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
On Thursday, 5 June 2014 at 18:51:25 UTC, George Sapkin wrote:
 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.
try this: dmd -lib test.d -defaultlib=libphobos2.a -oflibtest.a gcc main.c libtest.a -l:libphobos2.a -lpthread -lm -lrt
Jun 05 2014
parent reply "George Sapkin" <george.sapkin gmail.com> writes:
On Friday, 6 June 2014 at 02:13:08 UTC, Ellery Newcomer wrote:
 On Thursday, 5 June 2014 at 18:51:25 UTC, George Sapkin wrote:
 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.
try this: dmd -lib test.d -defaultlib=libphobos2.a -oflibtest.a gcc main.c libtest.a -l:libphobos2.a -lpthread -lm -lrt
Awesome! That works! Thanks. So is dmd linking to shared/different phobos2 by default or something?
Jun 05 2014
parent "Ellery Newcomer" <ellery-newcomer utulsa.edu> writes:
On Friday, 6 June 2014 at 02:17:50 UTC, George Sapkin wrote:
 On Friday, 6 June 2014 at 02:13:08 UTC, Ellery Newcomer wrote:
 On Thursday, 5 June 2014 at 18:51:25 UTC, George Sapkin wrote:
 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.
try this: dmd -lib test.d -defaultlib=libphobos2.a -oflibtest.a gcc main.c libtest.a -l:libphobos2.a -lpthread -lm -lrt
Awesome! That works! Thanks. So is dmd linking to shared/different phobos2 by default or something?
yeah, -defaultlib=libphobos2.so is the other option, I guess it's default now.
Jun 05 2014