Calling D from C

CJS Prometheus85 at hotmail.com
Sat Nov 23 21:25:27 PST 2013


I haven't been able to make calling D from C on Mac OS 10.9 work. 
I tried the following simple example:

foo.d

import std.stdio;

extern(C) int add(int x, int y){
     return x + y;
}

bar.c

#include <stdio.h>

int add(int , int);

int main(){

     int x = 1;
     int y = 2;

     char s[] = "%d + %d = %d";
     printf(s, x, y, add(x,y));
}

bash calls:
dmd -c foo.d
gcc bar.c foo.o

This gives a long error, summarized as
ld: symbol(s) not found for architecture x86_64

(FYI, in Mac OS 10.9 the aging gcc version has apparently been 
replaced with a more recent version of clang, so the above gcc 
call is actually some version of clang:

gcc --version
Configured with: 
--prefix=/Applications/Xcode.app/Contents/Developer/usr 
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix)

Is there some way to make this work? I'd like to call D code from 
C, and I'm fine statically compiling the D code and linking it to 
C.


More information about the Digitalmars-d-learn mailing list