digitalmars.D.learn - Calling D from C
- CJS (34/34) Nov 23 2013 I haven't been able to make calling D from C on Mac OS 10.9 work.
- evilrat (3/6) Nov 23 2013 this is wrong. there should flag for building static lib which
- Jesse Phillips (7/15) Nov 23 2013 There isn't anything special about .a files, they just contain
- evilrat (4/20) Nov 23 2013 this is not about runtime, ld(linker) reports missing symbols, so
- evilrat (9/11) Nov 23 2013 ok i find out what's your problem. here is 2 ways of solving this
- CJS (2/15) Nov 23 2013 That works. Thanks!
- Jacob Carlborg (5/11) Nov 24 2013 Compiling with DMD and the -v flag shows a verbose output and exactly
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
On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:bash calls: dmd -c foo.d gcc bar.c foo.othis is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
 Nov 23 2013
On Sunday, 24 November 2013 at 05:54:44 UTC, evilrat wrote:On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote: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.bash calls: dmd -c foo.d gcc bar.c foo.othis is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
 Nov 23 2013
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: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" keywordOn Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote: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.bash calls: dmd -c foo.d gcc bar.c foo.othis is wrong. there should flag for building static lib which should produce foo.a which then you link with C build.
 Nov 23 2013
On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:dmd -c foo.d gcc bar.c foo.ook 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
On Sunday, 24 November 2013 at 07:22:37 UTC, evilrat wrote:On Sunday, 24 November 2013 at 05:25:36 UTC, CJS wrote:That works. Thanks!dmd -c foo.d gcc bar.c foo.ook 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
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








 
  
  
 
 "evilrat" <evilrat666 gmail.com>
 "evilrat" <evilrat666 gmail.com> 