Calling D function from C

Daniel Worthington daniel.worthington at gmail.com
Mon Oct 11 17:32:28 PDT 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20101011/f394a7f5/attachment.html>


More information about the Digitalmars-d-learn mailing list