Calling D function from C

Jonathan Crapuchettes jcrapuchettes at gmail.com
Tue Apr 22 15:34:26 PDT 2008


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...


More information about the Digitalmars-d-learn mailing list