www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Linking C and D

reply "monarch_dodra" <monarchdodra gmail.com> writes:
I'm trying to get the hello world of cross compiling working:

main.d
//----
extern (C) void foo();
void main()
{
     foo();
}
//----

foo.c
//----
#include <stdio.h>

void foo()
{
   printf("hello world");
}
//----

I can't seem to get the executable to link correctly.

I'm using gcc and dmd on windows.

I'm building foo.c with:
gcc -c foo.c -o foo.obj

Then I build my exe with:
dmd foo.obj main.d

But I get:
OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
test.obj Offset 00000H Record Type 004C
  Error 138: Module or Dictionary corrupt
--- errorlevel 1

...Help ?
Feb 27 2013
next sibling parent reply "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Wednesday, 27 February 2013 at 16:12:13 UTC, monarch_dodra 
wrote:
 I'm trying to get the hello world of cross compiling working:
The short story is you can't link GCC and DMD object files on win32 because DMD emits OMF, GCC emits COFF, these are incompatible. You might want to read this: http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
 But I get:
 OPTLINK (R) for Win32  Release 8.00.12
 Copyright (C) Digital Mars 1989-2010  All rights reserved.
 http://www.digitalmars.com/ctg/optlink.html
 test.obj Offset 00000H Record Type 004C
  Error 138: Module or Dictionary corrupt
 --- errorlevel 1
I think we should try to implement a check in Optlink so it errors with a nicer "cannot link objects files in COFF format" rather than what it does right now.
Feb 27 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 27 February 2013 at 16:20:43 UTC, Andrej Mitrovic 
wrote:
 On Wednesday, 27 February 2013 at 16:12:13 UTC, monarch_dodra 
 wrote:
 I'm trying to get the hello world of cross compiling working:
The short story is you can't link GCC and DMD object files on win32 because DMD emits OMF, GCC emits COFF, these are incompatible. You might want to read this: http://www.gamedev.net/blog/1140/entry-2254003-binding-d-to-c/
 But I get:
 OPTLINK (R) for Win32  Release 8.00.12
 Copyright (C) Digital Mars 1989-2010  All rights reserved.
 http://www.digitalmars.com/ctg/optlink.html
 test.obj Offset 00000H Record Type 004C
 Error 138: Module or Dictionary corrupt
 --- errorlevel 1
I think we should try to implement a check in Optlink so it errors with a nicer "cannot link objects files in COFF format" rather than what it does right now.
Thanks.
Feb 27 2013
prev sibling next sibling parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
27.02.2013 23:12, monarch_dodra пишет:
 I can't seem to get the executable to link correctly.

 I'm using gcc and dmd on windows.

 I'm building foo.c with:
 gcc -c foo.c -o foo.obj

 Then I build my exe with:
 dmd foo.obj main.d

 But I get:
 OPTLINK (R) for Win32  Release 8.00.12
 Copyright (C) Digital Mars 1989-2010  All rights reserved.
 http://www.digitalmars.com/ctg/optlink.html
 test.obj Offset 00000H Record Type 004C
   Error 138: Module or Dictionary corrupt
 --- errorlevel 1

 ...Help ?
Under Windows I use dmc + dmd and gdc + gcc - all these combinations work fine. But I only tested it on simple and small files. There is also unilink (ftp://ftp.styx.cabel.net/pub/UniLink/) - they say it can link together omf and coff, but I don't try it
Feb 27 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 27 February 2013 at 16:37:06 UTC, Alexandr 
Druzhinin wrote:
 27.02.2013 23:12, monarch_dodra пишет:
 I can't seem to get the executable to link correctly.

 I'm using gcc and dmd on windows.

 I'm building foo.c with:
 gcc -c foo.c -o foo.obj

 Then I build my exe with:
 dmd foo.obj main.d

 But I get:
 OPTLINK (R) for Win32  Release 8.00.12
 Copyright (C) Digital Mars 1989-2010  All rights reserved.
 http://www.digitalmars.com/ctg/optlink.html
 test.obj Offset 00000H Record Type 004C
  Error 138: Module or Dictionary corrupt
 --- errorlevel 1

 ...Help ?
Under Windows I use dmc + dmd and gdc + gcc - all these combinations work fine. But I only tested it on simple and small files. There is also unilink (ftp://ftp.styx.cabel.net/pub/UniLink/) - they say it can link together omf and coff, but I don't try it
TY. dmc works. Unfortunatly, I'm actually doing this to compile "font" files into my D project. They can reach 2_000_000 lines, and weight in at over 100Mo of code. dmc seems to choke on these: //---- nbytes = 102000, ph_maxsize = 65520 Internal error: ph.c 1854 //---- dmc seems to start choking when my font files start to reach about 15 Mo. I suppose there's a switch somewhere, but I've never used dmc before, so all the switches are unknown to me. I guess I'll just have to learn (anybody know?). If worst comes to worst, I can try to split the font files, but since they are tool generated, I really shouldn't be touching them.
Feb 27 2013
parent reply Alexandr Druzhinin <drug2004 bk.ru> writes:
27.02.2013 23:50, monarch_dodra пишет:
 dmc seems to start choking when my font files start to reach about 15 Mo.

 I suppose there's a switch somewhere, but I've never used dmc before, so
 all the switches are unknown to me. I guess I'll just have to learn
 (anybody know?).

 If worst comes to worst, I can try to split the font files, but since
 they are tool generated, I really shouldn't be touching them.
IIRC, there is such switch, but I can't remember it, may be http://www.digitalmars.com/ctg/sc.html#dashCapHCapP ?
Feb 27 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 27 February 2013 at 17:08:38 UTC, Alexandr
Druzhinin wrote:
 27.02.2013 23:50, monarch_dodra пишет:
 dmc seems to start choking when my font files start to reach 
 about 15 Mo.

 I suppose there's a switch somewhere, but I've never used dmc 
 before, so
 all the switches are unknown to me. I guess I'll just have to 
 learn
 (anybody know?).

 If worst comes to worst, I can try to split the font files, 
 but since
 they are tool generated, I really shouldn't be touching them.
IIRC, there is such switch, but I can't remember it, may be http://www.digitalmars.com/ctg/sc.html#dashCapHCapP ?
I found a couple posts about that, but it doesn't seem to do much. I'll just have to try harder.
Feb 27 2013
parent "jerro" <a a.com> writes:
On Wednesday, 27 February 2013 at 18:40:40 UTC, monarch_dodra 
wrote:
 On Wednesday, 27 February 2013 at 17:08:38 UTC, Alexandr
 Druzhinin wrote:
 27.02.2013 23:50, monarch_dodra пишет:
 dmc seems to start choking when my font files start to reach 
 about 15 Mo.

 I suppose there's a switch somewhere, but I've never used dmc 
 before, so
 all the switches are unknown to me. I guess I'll just have to 
 learn
 (anybody know?).

 If worst comes to worst, I can try to split the font files, 
 but since
 they are tool generated, I really shouldn't be touching them.
IIRC, there is such switch, but I can't remember it, may be http://www.digitalmars.com/ctg/sc.html#dashCapHCapP ?
I found a couple posts about that, but it doesn't seem to do much. I'll just have to try harder.
You could try using GDC and GCC instead. There are MinGW GDC builds at https://bitbucket.org/goshawk/gdc/downloads .
Feb 27 2013
prev sibling next sibling parent "Michael" <pr m1xa.com> writes:
With Dmd you can use dmc directly.

Second way is: you can compile your code as dll library and then 
create import library with implib utility for win32, for win64 a 
import library can be used directly.

First way works good, but I prefer second one.
Both approaches for win32 and win64 works good enough and stable.
Feb 27 2013
prev sibling parent "Michael" <pr m1xa.com> writes:
Also C runtime can be statically compiled with your c/dll 
library. You just need copy c dll and your d exe without 
additional dependencies.

C dll can be prepared by visual c.
Feb 27 2013