www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling D function from C

reply Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
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...
Apr 22 2008
next sibling parent Johan Granberg <lijat.meREM OVEgmail.com> writes:
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)
Apr 22 2008
prev sibling next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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.

You might be able to learn something from looking at PyD. http://pyd.dsource.org/ That aims to let you write Python extensions in D. It works by making shared libs though. That might be the best way. I think that means you would have to use GDC on linux. --bb
Apr 23 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jonathan Crapuchettes" <jcrapuchettes gmail.com> wrote in message 
news:fulp5j$19hu$1 digitalmars.com...

 /opt/dmd/bin/dmd test.d -c
 gcc main.c test.o -m32 -o tester -lphobos -lpthread -lm
 Linker Error...

What _was_ the linker error?
Apr 23 2008
parent Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
/usr/lib/gcc/x86_64-linux-gnu/4.1.3/../../../../lib32/libphobos.a(deh2.o): In 
function `_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
internal/deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): 
undefined reference to `_deh_beg'
internal/deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): 
undefined reference to `_deh_beg'
internal/deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): 
undefined reference to `_deh_end'
internal/deh2.d:(.text._D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): 
undefined reference to `_deh_end'
collect2: ld returned 1 exit status

Jarrett Billingsley wrote:
 "Jonathan Crapuchettes" <jcrapuchettes gmail.com> wrote in message 
 news:fulp5j$19hu$1 digitalmars.com...
 
 /opt/dmd/bin/dmd test.d -c
 gcc main.c test.o -m32 -o tester -lphobos -lpthread -lm
 Linker Error...

What _was_ the linker error?

Apr 23 2008
prev sibling parent Sivo Schilling <sivo.schilling web.de> writes:
Content-Type: text/plain

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

Apr 23 2008