www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling D from C

reply "CJS" <Prometheus85 hotmail.com> writes:
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.
Nov 23 2013
next sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:
 bash calls:
 dmd -c foo.d
 gcc bar.c foo.o
this is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
Nov 23 2013
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Sunday, 24 November 2013 at 05:54:44 UTC, evilrat wrote:
 On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:
 bash calls:
 dmd -c foo.d
 gcc bar.c foo.o
this is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
There isn't anything special about .a files, they just contain multiple .o "collections" He looks to be missing an initialization of the runtime, but then again thought I read something about a trick making that not needed (at least for dynamic loading)... Someone else will have to pitch in what the correct solution is.
Nov 23 2013
parent "evilrat" <evilrat666 gmail.com> writes:
On Sunday, 24 November 2013 at 06:48:50 UTC, Jesse Phillips wrote:
 On Sunday, 24 November 2013 at 05:54:44 UTC, evilrat wrote:
 On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:
 bash calls:
 dmd -c foo.d
 gcc bar.c foo.o
this is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
There isn't anything special about .a files, they just contain multiple .o "collections" He looks to be missing an initialization of the runtime, but then again thought I read something about a trick making that not needed (at least for dynamic loading)... Someone else will have to pitch in what the correct solution is.
this is not about runtime, ld(linker) reports missing symbols, so it is either x86/x64 versions mismatch, or maybe with dmd 2.064 it is necessary to mark exported symbols with "export" keyword
Nov 23 2013
prev sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:
 dmd -c foo.d
 gcc bar.c foo.o
ok i find out what's your problem. here is 2 ways of solving this problem. 1) build like you do already but add to gcc call phobos lib, so it will looks like "gcc bar.c foo.o /usr/share/dmd/lib/libphobos2.a" 2) build with foo.d with "dmd foo.d -lib" which generates proper lib file with all necessary stuff from phobos and whatever. i specifically choose this order so you can think whats happened.
Nov 23 2013
next sibling parent "CJS" <Prometheus85 hotmail.com> writes:
On Sunday, 24 November 2013 at 07:22:37 UTC, evilrat wrote:
 On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:
 dmd -c foo.d
 gcc bar.c foo.o
ok i find out what's your problem. here is 2 ways of solving this problem. 1) build like you do already but add to gcc call phobos lib, so it will looks like "gcc bar.c foo.o /usr/share/dmd/lib/libphobos2.a" 2) build with foo.d with "dmd foo.d -lib" which generates proper lib file with all necessary stuff from phobos and whatever. i specifically choose this order so you can think whats happened.
That works. Thanks!
Nov 23 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-11-24 08:22, evilrat wrote:

 ok i find out what's your problem. here is 2 ways of solving this problem.

 1) build like you do already but add to gcc call phobos lib, so it will
 looks like "gcc bar.c foo.o /usr/share/dmd/lib/libphobos2.a"

 2) build with foo.d with "dmd foo.d -lib" which generates proper lib
 file with all necessary stuff from phobos and whatever.

 i specifically choose this order so you can think whats happened.
Compiling with DMD and the -v flag shows a verbose output and exactly how DMD calls GCC to link everything. -- /Jacob Carlborg
Nov 24 2013