Calling D function from C
Johan Granberg
lijat.meREM at OVEgmail.com
Tue Apr 22 23:36:26 PDT 2008
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...
The code looks ok to me, wath if you compile the c code first and then use
dmd to link it (dmd probably uses some linker flags you did not think of)
More information about the Digitalmars-d
mailing list