digitalmars.D.learn - Using D static library from C
- George Sapkin (38/38) Jun 05 2014 I'm trying to link a simple D static library to C code, but I'm
- Stefan Koch (1/1) Jun 05 2014 You need to link the druntime too, I think.
- George Sapkin (2/3) Jun 05 2014 At which stage? Can you provide a full command? Thanks.
- Stefan Koch (5/8) Jun 05 2014 Err ... I just remeberd linking d with gcc needs a few libs
- George Sapkin (4/13) Jun 05 2014 I can link a D object (not a static lib) with gcc using -lphobos2
- George Sapkin (4/13) Jun 05 2014 I can link a D object (not a static lib) with gcc using -lphobos2
- Mark Isaacson (7/7) Jun 05 2014 I found that if I told dmd rather than gcc to do the linking that
- George Sapkin (9/18) Jun 05 2014 Yes, but I need to integrate this with gcc project. To get around
- Dave Wilson (3/3) Jun 05 2014 You can create a static library from one or more .o files using
- George Sapkin (3/6) Jun 05 2014 I don't have trouble creating a static library with dmd, I have
- Ellery Newcomer (4/7) Jun 05 2014 try this:
- George Sapkin (4/11) Jun 05 2014 Awesome! That works! Thanks.
- Ellery Newcomer (3/16) Jun 05 2014 yeah, -defaultlib=libphobos2.so is the other option, I guess it's
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
You need to link the druntime too, I think.
Jun 05 2014
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
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: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 dmdYou need to link the druntime too, I think.At which stage? Can you provide a full command? Thanks.
Jun 05 2014
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: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.On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote: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 dmdYou need to link the druntime too, I think.At which stage? Can you provide a full command? Thanks.
Jun 05 2014
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: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.On Thursday, 5 June 2014 at 18:55:13 UTC, Stefan Koch wrote: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 dmdYou need to link the druntime too, I think.At which stage? Can you provide a full command? Thanks.
Jun 05 2014
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
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
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
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
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
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:Awesome! That works! Thanks. So is dmd linking to shared/different phobos2 by default or something?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
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:yeah, -defaultlib=libphobos2.so is the other option, I guess it's default now.On Thursday, 5 June 2014 at 18:51:25 UTC, George Sapkin wrote:Awesome! That works! Thanks. So is dmd linking to shared/different phobos2 by default or something?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