Calling D function from C

Sivo Schilling sivo.schilling at web.de
Wed Apr 23 09:25:19 PDT 2008


Reply to Jonathan:

I investigated this problem some days ago to find a way for such
callbacks from C code to D code. See the attached files cexception.c
and dexception.d. 
Build the executable (Windows & DMD 1.028 or DMD 2.012 & DM 8.50) 
via

sc -c cexcexption.c
dmd -c dexception.d
link cexcexption.obj+dexcexption.obj, dcexception.exe

Good luck
Sivo.

Jonathan Crapuchettes Wrote:

> Is there a way to compile a D object file with some C code? I am looking into 
> creating a PHP extension from D instead of C and as I see it, I would need to be 
> able to create and interface function in C that can call a D function.
> 
> I tried a simple example of this, but ran into some linking problems. The 
> example follows:
> 
> ------------- D code -----------------
> import std.stdio;
> 
> extern (C)
> {
>      void test()
>      {
> 	test2();
>      }
> 
> } //end extern
> 
> void test2()
> {
>      writefln("test");
> }
> 
> ------------- C code -------------------
> #include <stdio.h>
> 
> void test();
> 
> int main(int argc, char* argv[])
> {
>      test();
> 
>      return 0;
> }
> 
> ------------ Compiled As ---------------
> /opt/dmd/bin/dmd test.d -c
> gcc main.c test.o -m32 -o tester -lphobos -lpthread -lm
> Linker Error...

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: cexcept.c
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20080423/f3e68efc/attachment.c>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: dexcept.d
Type: text/x-dsrc
Size: 1700 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20080423/f3e68efc/attachment.d>


More information about the Digitalmars-d mailing list