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 reply 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
parent reply "Bruce Adams" <tortoise_74 yeah.who.co.uk> writes:
On Wed, 23 Apr 2008 07:36:26 +0100, Johan Granberg  
<lijat.meREM OVEgmail.com> wrote:

 Jonathan Crapuchettes wrote:
 ------------ 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)
Although in theory both compilers are generating compatible ELF objects I would be wary of mixing compilers this way and much more comfortable using either dmc & dmc or gdc & gcc together.
Apr 23 2008
parent Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
I tried both gdc and dmd for the D code and both had gcc create the same linker 
errors.
Thanks
JC

Bruce Adams wrote:
 On Wed, 23 Apr 2008 07:36:26 +0100, Johan Granberg 
 <lijat.meREM OVEgmail.com> wrote:
 
 Jonathan Crapuchettes wrote:
 ------------ 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)
Although in theory both compilers are generating compatible ELF objects I would be wary of mixing compilers this way and much more comfortable using either dmc & dmc or gdc & gcc together.
Apr 23 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 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 reply 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
parent reply BCS <BCS pathlink.com> writes:
Jonathan Crapuchettes wrote:
 /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?
`_deh_beg' & `_deh_end' (or should that be da_beg & da_end) are IIRC part of the runtime. Try deleting all the .o files and then running it the other way (gcc -c main.c; dmd test.d main.o) dmd sometimes has issues with stuff if you compile piecemeal. There might be other ways to fix this but I don't recall the details.
Apr 23 2008
next sibling parent Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
I got the same errors doing you suggestion.
Thanks,
JC

gcc -c main.c -m32
jonathan blackbeard:~/workspace/c/ctodTest$ /opt/dmd/bin/dmd test.d main.o
gcc test.o main.o -o test -m32 -Xlinker -L/opt/dmd/bin/../lib -lphobos
-lpthread -lm
Errors...

BCS wrote:
 Jonathan Crapuchettes wrote:
 /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?
`_deh_beg' & `_deh_end' (or should that be da_beg & da_end) are IIRC part of the runtime. Try deleting all the .o files and then running it the other way (gcc -c main.c; dmd test.d main.o) dmd sometimes has issues with stuff if you compile piecemeal. There might be other ways to fix this but I don't recall the details.
Apr 23 2008
prev sibling parent Jonathan Crapuchettes <jcrapuchettes gmail.com> writes:
I just tried the following and it worked.

gcc -c main.c
gdmd test.d main.o

Thanks for everyones help!
JC

BCS wrote:
 Jonathan Crapuchettes wrote:
 /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?
`_deh_beg' & `_deh_end' (or should that be da_beg & da_end) are IIRC part of the runtime. Try deleting all the .o files and then running it the other way (gcc -c main.c; dmd test.d main.o) dmd sometimes has issues with stuff if you compile piecemeal. There might be other ways to fix this but I don't recall the details.
Apr 23 2008