Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 05, 2014 Using D static library from C | ||||
---|---|---|---|---|
| ||||
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. |
June 05, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to George Sapkin | You need to link the druntime too, I think. |
June 05, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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.
|
June 05, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to George Sapkin | 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. |
June 05, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to George Sapkin | 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
|
June 06, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dave Wilson | 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.
|
June 06, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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.
|
June 06, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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.
|
June 06, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to George Sapkin | 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. |
June 06, 2014 Re: Using D static library from C | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Isaacson | 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.
|
Copyright © 1999-2021 by the D Language Foundation