Linking D code into existing C programs

Johan Engelen via Digitalmars-d digitalmars-d at puremagic.com
Tue Sep 27 03:46:23 PDT 2016


On Monday, 26 September 2016 at 23:32:05 UTC, Walter Bright wrote:
> Linking C libraries and object code into D programs has always 
> worked easily in D. The other way around, not so well.
>
> [snip]
>
> How much of an issue is this with D? Is it something we need to 
> address?

We've been toying with this in setting up LDC's build such that 
it works on different platforms. Wait, "toying" implies "fun". It 
was anything but.
At first, we used the D compiler to do the final linking, but it 
resulted in troubles when special linker flags are needed.
We've now moved to using the system linker separately to do the 
linking.
https://github.com/ldc-developers/ldc/pull/1594

For this, we use `-v` to figure out what the system linker is and 
what linker flags are passed by the D compiler (link with phobos, 
druntime, etc.). But it needs "parsing" of `-v` output, quite 
annoying.
See 
https://github.com/ldc-developers/ldc/blob/master/cmake/Modules/ExtractDMDSystemLinker.cmake

The idea to add a cmdline flag `-ldflags` that would just output 
the linking flags, did not get a response.
https://forum.dlang.org/post/gqaujnbgbpauirbezjki@forum.dlang.org
I think it will save people a lot of time and frustration. 
(possible improvements can be made such that the output of 
`-ldflags` also includes extra link flags when special compiler 
flags are passed, such as `-fprofile-instr-generate`.)

-Johan


More information about the Digitalmars-d mailing list