April 22, 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...
April 23, 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)
April 23, 2008
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.
April 23, 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.

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
April 23, 2008
"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?


April 23, 2008
/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? 
> 
> 
April 23, 2008
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.
April 23, 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...



April 23, 2008
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.
April 23, 2008
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.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home