Linking D code into existing C programs
ZombineDev via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 28 01:38:27 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.
>
> Consider the following C program:
>
> ---- main.c ----
> extern int foo();
> void main(int argc, char* argv[]) {
> foo();
> }
> ---- foo.c ----
> int foo() { return 7; }
> ---------------
>
> Compile/link in the usual way:
>
> gcc main.c -c
> gcc foo.c -c
> gcc main.o foo.o
>
> No problem.
> Now replace foo.c with foo.d:
>
> ---- foo.d ----
> extern (C) int foo() { return 7; }
> ---------------
>
> Compile/link with:
>
> gcc main.c -c
> dmd foo.d -c
> gcc main.o foo.o
>
> Produces:
>
> bar.o:(.eh_frame+0x13): undefined reference to
> `__dmd_personality_v0'
> bar.o: In function `_D3bar7__arrayZ':
> bar.d:(.text._D3bar7__arrayZ+0x21): undefined reference to
> `_d_arraybounds'
> bar.o: In function `_D3bar8__assertFiZv':
> bar.d:(.text._D3bar8__assertFiZv+0x21): undefined reference
> to `_d_assert'
> bar.o: In function `_D3bar15__unittest_failFiZv':
> bar.d:(.text._D3bar15__unittest_failFiZv+0x21): undefined
> reference to `_d_unittest'
> bar.o: In function `__d_dso_init':
> bar.d:(.text.d_dso_init[.data.d_dso_rec]+0x28): undefined
> reference to `_d_dso_registry'
> collect2: error: ld returned 1 exit status
>
> How much of an issue is this with D? Is it something we need to
> address?
Many users that want/need to use D without druntime, which
implies nothrow @nogc, --boundscheck=off, no unittests (and
probably a couple of other things) and we need to support these
use cases. In other words,
the compiler should detect if a given module does not use any
druntime features and in such cases it should not emit calls/
linktime references to druntime. I don't know if in general the
compiler can tell if certain runtime features (e.g. exception
handling support) are not used needed, so at first we may require
the -betterC switch to supplied.
More information about the Digitalmars-d
mailing list