Undefined Reference calling D from C using static linking

data pulverizer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Mar 23 02:11:28 PDT 2017


I am trying to call a D function from C. Here is the D code:

```
/* dcode.d */
extern (C) nothrow @nogc @system {
     double multNum(double x, double y)
     {
         return x*y;
     }
}
```

Then the C code:

```
/* ccode.c */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

extern double multNum(double x, double y);

int main()
{
     printf("output: %f", multNum(3.0, 4.0));
     return 0;
}

```

Then I compile with:

```
ldc2 -c dcode.d
gcc -c ccode.c
gcc -o output ccode.o dcode.o
```

I get the error:

```
dcode.o: In function `ldc.register_dso':
dcode.d:(.text.ldc.register_dso+0x6e): undefined reference to 
`_d_dso_registry'
collect2: error: ld returned 1 exit status
```

Compiler versions:

```
$ ldc2 --version
LDC - the LLVM D compiler (1.1.0):
   based on DMD v2.071.2 and LLVM 3.9.1
   built with LDC - the LLVM D compiler (1.1.0)
   Default target: x86_64-unknown-linux-gnu
   Host CPU: ivybridge
   http://dlang.org - http://wiki.dlang.org/LDC

   Registered Targets:
     x86    - 32-bit X86: Pentium-Pro and above
     x86-64 - 64-bit X86: EM64T and AMD64

```

```
$ gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  
There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE.
```

I would appreciate it if someone could point out my mistake. 
Thank you in advance



More information about the Digitalmars-d-learn mailing list