Calling D function from C

Denis Koroskin 2korden at gmail.com
Mon Oct 11 17:41:05 PDT 2010


On Tue, 12 Oct 2010 04:32:28 +0400, Daniel Worthington  
<daniel.worthington at 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).


More information about the Digitalmars-d-learn mailing list