digitalmars.D.learn - Calling D function from C
- Daniel Worthington <daniel.worthington gmail.com> Oct 11 2010
- "Denis Koroskin" <2korden gmail.com> Oct 11 2010
- bearophile <bearophileHUGS lycos.com> Oct 11 2010
- Daniel Worthington <daniel.worthington gmail.com> Oct 11 2010
- "Denis Koroskin" <2korden gmail.com> Oct 12 2010
- Emil Madsen <sovende gmail.com> Oct 12 2010
- "Denis Koroskin" <2korden gmail.com> Oct 12 2010
- Emil Madsen <sovende gmail.com> Oct 18 2010
--90e6ba3fd217956b990492609cd8 Content-Type: text/plain; charset=ISO-8859-1 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 --90e6ba3fd217956b990492609cd8 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>I'm trying to set up a project where I can call D functions from C= code. Based on the documentation (<a href=3D"http://www.digitalmars.com/d/= 2.0/interfaceToC.html">http://www.digitalmars.com/d/2.0/interfaceToC.html</= a>) it looks like I should be able to do the following:</div> <div><br></div>dee.d:<div>- - - - - - - -<br><div><div>import std.stdio;</d= iv><div><br></div><div><div>extern(C)</div><div>{</div><div>=A0=A0 =A0void = sayHelloFromD()</div><div>=A0=A0 =A0{</div><div>=A0=A0 =A0 =A0 =A0writeln(&= quot;Hello from D.");</div> <div>=A0=A0 =A0}</div><div>}</div></div><div>- - - - - - - -</div><div><br>= </div><div><br></div><div>dee.h</div><div>- - - - - - - -</div><div><div>vo= id sayHelloFromD();</div></div><div>- - - - - - - -</div><div><br></div><di= v> <br></div><div>cee.c</div><div>- - - - - - - -</div><div>#include <stdio= .h></div><div><div>#include "dee.h";</div><div><br></div><div>= int main()</div><div>{</div><div>=A0=A0 =A0printf("Hello from C.\n&quo= t;);</div> <div>=A0=A0 =A0sayHelloFromD();</div><div>=A0=A0 =A0return 0;</div><div>}</= div></div><div>- - - - - - - -</div><div><br></div></div><div><br></div><di= v>To compile:</div><div>dmd -c dee.d</div><div>gcc dee.o cee.c -arch i386 -= o hello</div> <div><br></div><div>It fails with the following:</div><div><div>Undefined s= ymbols:</div><div>=A0=A0"_D3std5stdio6stdoutS3std5stdio4File", re= ferenced from:</div><div>=A0=A0 =A0 =A0_D3std5stdio6stdoutS3std5stdio4File$= non_lazy_ptr in dee.o</div> <div>=A0=A0 =A0 (maybe you meant: _D3std5stdio6stdoutS3std5stdio4File$non_l= azy_ptr)</div><div>=A0=A0"_D15TypeInfo_Struct6__vtblZ", reference= d from:</div><div>=A0=A0 =A0 =A0_D47TypeInfo_S3std6traits15__T8DemangleTkZ8= Demangle6__initZ in dee.o</div> <div>=A0=A0"_D3std9contracts7bailOutFAyaixAaZv", referenced from:= </div><div>=A0=A0 =A0 =A0_D3std9contracts112__T7enforceTbVAyaa42_2f6f70742f= 646d64322f62696e2f2e2e2f2e2e2f7372632f70686f626f732f7374642f737464696f2e64V= i1332Z7enforceFbLAxaZb in dee.o</div> <div>=A0=A0"_D3std5stdio12__ModuleInfoZ", referenced from:</div><= div>=A0=A0 =A0 =A0_D3dee12__ModuleInfoZ in dee.o</div><div>ld: symbol(s) no= t found</div><div>collect2: ld returned 1 exit status</div></div><div><br><= /div><div> <br></div><div>I would appreciate any help you can give me with this.</div>= <div><br></div><div>Daniel</div></div> --90e6ba3fd217956b990492609cd8--
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. Daniel
You 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
--20cf301d4266635e04049263a8ab Content-Type: text/plain; charset=ISO-8859-1 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 is
and implemented).
And maybe use dmc instead of gcc if the compilation is done on Windows. Bye, bearophile
--20cf301d4266635e04049263a8ab Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Linking with the phobos lib got it to compile, but I get a Bus Error when r= unning: 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.<div> <br></div><div>Do you think this is an issue of using dmd and gcc together?= I'm on Mac OS X.</div><div><br></div><div>Daniel<br><br><div class=3D"= gmail_quote">On Mon, Oct 11, 2010 at 6:17 PM, bearophile <span dir=3D"ltr">= <<a href=3D"mailto:bearophileHUGS lycos.com">bearophileHUGS lycos.com</a=></span> wrote:<br>
x #ccc solid;padding-left:1ex;">Denis Koroskin:<br> <div class=3D"im"><br> > You need to link with phobos.lib (because this is where writeln is def= ined<br> > and implemented).<br> <br> </div>And maybe use dmc instead of gcc if the compilation is done on Window= s.<br> <br> Bye,<br> <font color=3D"#888888">bearophile<br> </font></blockquote></div><br></div> --20cf301d4266635e04049263a8ab--
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. Daniel
I 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
--0015174bdea05caf0a049266ad71 Content-Type: text/plain; charset=ISO-8859-1 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 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. Daniel
I 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.
-- // Yours sincerely // Emil 'Skeen' Madsen --0015174bdea05caf0a049266ad71 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable wouldn't compiling, without garbage collector help too?<br><br><div cla= ss=3D"gmail_quote">2010/10/12 Denis Koroskin <span dir=3D"ltr"><<a href= =3D"mailto:2korden gmail.com">2korden gmail.com</a>></span><br><blockquo= te class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc so= lid;padding-left:1ex;"> <div class=3D"im">On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington &l= t;<a href=3D"mailto:daniel.worthington gmail.com" target=3D"_blank">daniel.= worthington gmail.com</a>> wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Linking with the phobos lib got it to compile, but I get a Bus Error when<b= r> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking a= t<br> the crash reports, it looks like this happens whenever the D code tries to<= br> allocate memory.<br> <br> Do you think this is an issue of using dmd and gcc together? I'm on Mac= OS<br> X.<br> <br> Daniel<br> </blockquote> <br></div> I think that's because you skipped some initialization for D.<br> At the very least, a GC needs to be initialized first.<br> <br> Try calling gc_init() (defined in core.memory) and thread_attachThis() (cor= e.thread) because invoking other D code.<br> <br> That should help.<br> </blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincerely<br>/= / Emil 'Skeen' Madsen<br> --0015174bdea05caf0a049266ad71--
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>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. Daniel
I 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.
It would, but then are you okay with memory leaks (i.e. writeln might allocate something)?
Oct 12 2010
--000e0cd1d7500f1ed60492e279a0 Content-Type: text/plain; charset=ISO-8859-1 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?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 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. Daniel
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.
allocate something)?
-- // Yours sincerely // Emil 'Skeen' Madsen --000e0cd1d7500f1ed60492e279a0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hopefully one could use printf, and tbh I dont like the hole garbage collec= tor thingy :)<br><br><div class=3D"gmail_quote">2010/10/12 Denis Koroskin <= span dir=3D"ltr"><<a href=3D"mailto:2korden gmail.com">2korden gmail.com= </a>></span><br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;"><div class=3D"im">On Tue, 12 Oct 2010 11:46= :39 +0400, Emil Madsen <<a href=3D"mailto:sovende gmail.com" target=3D"_= blank">sovende gmail.com</a>> wrote:<br> <br> </div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-l= eft:1px #ccc solid;padding-left:1ex"><div class=3D"im"> wouldn't compiling, without garbage collector help too?<br> <br> 2010/10/12 Denis Koroskin <<a href=3D"mailto:2korden gmail.com" target= =3D"_blank">2korden gmail.com</a>><br> <br> </div><div><div class=3D"h5"><blockquote class=3D"gmail_quote" style=3D"mar= gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> On Tue, 12 Oct 2010 08:10:31 +0400, Daniel Worthington <<br> <a href=3D"mailto:daniel.worthington gmail.com" target=3D"_blank">daniel.wo= rthington gmail.com</a>> wrote:<br> <br> =A0Linking with the phobos lib got it to compile, but I get a Bus Error whe= n<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> running: EXC_BAD_ACCESS (SIGBUS). Playing with the code a bit and looking<b= r> at<br> the crash reports, it looks like this happens whenever the D code tries to<= br> allocate memory.<br> <br> Do you think this is an issue of using dmd and gcc together? I'm on Mac= OS<br> X.<br> <br> Daniel<br> <br> </blockquote> <br> I think that's because you skipped some initialization for D.<br> At the very least, a GC needs to be initialized first.<br> <br> Try calling gc_init() (defined in core.memory) and thread_attachThis()<br> (core.thread) because invoking other D code.<br> <br> That should help.<br> <br> </blockquote> <br> <br> <br> </div></div></blockquote> <br> It would, but then are you okay with memory leaks (i.e. writeln might alloc= ate something)?<br> </blockquote></div><br><br clear=3D"all"><br>-- <br>// Yours sincerely<br>/= / Emil 'Skeen' Madsen<br> --000e0cd1d7500f1ed60492e279a0--
Oct 18 2010









bearophile <bearophileHUGS lycos.com> 