digitalmars.D.learn - Calling D function from C
- Daniel Worthington (49/49) Oct 11 2010 I'm trying to set up a project where I can call D functions from C code.
- Denis Koroskin (4/55) Oct 11 2010 You need to link with phobos.lib (because this is where writeln is defin...
- bearophile (4/6) Oct 11 2010 And maybe use dmc instead of gcc if the compilation is done on Windows.
- Daniel Worthington (8/15) Oct 11 2010 Linking with the phobos lib got it to compile, but I get a Bus Error whe...
- Denis Koroskin (7/17) Oct 12 2010 I think that's because you skipped some initialization for D.
- Emil Madsen (5/23) Oct 12 2010 --
- Denis Koroskin (3/32) Oct 12 2010 It would, but then are you okay with memory leaks (i.e. writeln might
- Emil Madsen (6/45) Oct 18 2010 Hopefully one could use printf, and tbh I dont like the hole garbage
I'm trying to set up a project where I can call D functions from C code. Based on the documentation ( http://www.digitalmars.com/d/2.0/interfaceToC.html) it looks like I should be able to do the following: dee.d: - - - - - - - - import std.stdio; extern(C) { void sayHelloFromD() { writeln("Hello from D."); } } - - - - - - - - dee.h - - - - - - - - void sayHelloFromD(); - - - - - - - - cee.c - - - - - - - - #include <stdio.h> #include "dee.h"; int main() { printf("Hello from C.\n"); sayHelloFromD(); return 0; } - - - - - - - - To compile: dmd -c dee.d gcc dee.o cee.c -arch i386 -o hello It fails with the following: Undefined symbols: "_D3std5stdio6stdoutS3std5stdio4File", referenced from: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr in dee.o (maybe you meant: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr) "_D15TypeInfo_Struct6__vtblZ", referenced from: _D47TypeInfo_S3std6traits15__T8DemangleTkZ8Demangle6__initZ in dee.o "_D3std9contracts7bailOutFAyaixAaZv", referenced from: _D3std9contracts112__T7enforceTbVAyaa42_2f6f70742f646d64322f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1332Z7enforceFbLAxaZb in dee.o "_D3std5stdio12__ModuleInfoZ", referenced from: _D3dee12__ModuleInfoZ in dee.o ld: symbol(s) not found collect2: ld returned 1 exit status I would appreciate any help you can give me with this. Daniel
Oct 11 2010
On Tue, 12 Oct 2010 04:32:28 +0400, Daniel Worthington <daniel.worthington gmail.com> wrote:I'm trying to set up a project where I can call D functions from C code. Based on the documentation ( http://www.digitalmars.com/d/2.0/interfaceToC.html) it looks like I should be able to do the following: dee.d: - - - - - - - - import std.stdio; extern(C) { void sayHelloFromD() { writeln("Hello from D."); } } - - - - - - - - dee.h - - - - - - - - void sayHelloFromD(); - - - - - - - - cee.c - - - - - - - - #include <stdio.h> #include "dee.h"; int main() { printf("Hello from C.\n"); sayHelloFromD(); return 0; } - - - - - - - - To compile: dmd -c dee.d gcc dee.o cee.c -arch i386 -o hello It fails with the following: Undefined symbols: "_D3std5stdio6stdoutS3std5stdio4File", referenced from: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr in dee.o (maybe you meant: _D3std5stdio6stdoutS3std5stdio4File$non_lazy_ptr) "_D15TypeInfo_Struct6__vtblZ", referenced from: _D47TypeInfo_S3std6traits15__T8DemangleTkZ8Demangle6__initZ in dee.o "_D3std9contracts7bailOutFAyaixAaZv", referenced from: _D3std9contracts112__T7enforceTbVAyaa42_2f6f70742f646d64322f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64Vi1332Z7enforceFbLAxaZb in dee.o "_D3std5stdio12__ModuleInfoZ", referenced from: _D3dee12__ModuleInfoZ in dee.o ld: symbol(s) not found collect2: ld returned 1 exit status I would appreciate any help you can give me with this. DanielYou need to link with phobos.lib (because this is where writeln is defined and implemented).
Oct 11 2010
Denis Koroskin:You need to link with phobos.lib (because this is where writeln is defined and implemented).And maybe use dmc instead of gcc if the compilation is done on Windows. Bye, bearophile
Oct 11 2010
Linking with the phobos lib got it to compile, but I get a Bus Error when running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory. Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X. Daniel On Mon, Oct 11, 2010 at 6:17 PM, bearophile <bearophileHUGS lycos.com>wrote:Denis Koroskin:You need to link with phobos.lib (because this is where writeln isdefinedand implemented).And maybe use dmc instead of gcc if the compilation is done on Windows. Bye, bearophile
Oct 11 2010
On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <daniel.worthington gmail.com> wrote:Linking with the phobos lib got it to compile, but I get a Bus Error when running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory. Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X. DanielI think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first. Try calling gc_init() (defined in core.memory) and thread_attachThis() (core.thread) because invoking other D code. That should help.
Oct 12 2010
wouldn't compiling, without garbage collector help too? 2010/10/12 Denis Koroskin <2korden gmail.com>On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington < daniel.worthington gmail.com> wrote: Linking with the phobos lib got it to compile, but I get a Bus Error when-- // Yours sincerely // Emil 'Skeen' Madsenrunning: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory. Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X. DanielI think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first. Try calling gc_init() (defined in core.memory) and thread_attachThis() (core.thread) because invoking other D code. That should help.
Oct 12 2010
On Tue, 12 Oct 2010 11:46:39 +0400, Emil Madsen <sovende gmail.com> wrote:wouldn't compiling, without garbage collector help too? 2010/10/12 Denis Koroskin <2korden gmail.com>It would, but then are you okay with memory leaks (i.e. writeln might allocate something)?On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington < daniel.worthington gmail.com> wrote: Linking with the phobos lib got it to compile, but I get a Bus Error whenrunning: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory. Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X. DanielI think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first. Try calling gc_init() (defined in core.memory) and thread_attachThis() (core.thread) because invoking other D code. That should help.
Oct 12 2010
Hopefully one could use printf, and tbh I dont like the hole garbage collector thingy :) 2010/10/12 Denis Koroskin <2korden gmail.com>On Tue, 12 Oct 2010 11:46:39 +0400, Emil Madsen <sovende gmail.com> wrote: wouldn't compiling, without garbage collector help too?-- // Yours sincerely // Emil 'Skeen' Madsen2010/10/12 Denis Koroskin <2korden gmail.com> On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <It would, but then are you okay with memory leaks (i.e. writeln might allocate something)?daniel.worthington gmail.com> wrote: Linking with the phobos lib got it to compile, but I get a Bus Error whenrunning: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking at the crash reports, it looks like this happens whenever the D code tries to allocate memory. Do you think this is an issue of using dmd and gcc together? I'm on Mac OS X. DanielI think that's because you skipped some initialization for D. At the very least, a GC needs to be initialized first. Try calling gc_init() (defined in core.memory) and thread_attachThis() (core.thread) because invoking other D code. That should help.
Oct 18 2010