www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Calling C function

reply Tino <mvandenboogaard gmail.com> writes:
Hi,

I know it is possible to call a C function from D. It works on Linux. I 
don't have a very in depth knowledge of C and linking so please excuse 
my ignorence if I have missed something obvious.


I have the following two files:

------------------------------
#file mylib.c
int get_one()
{
    return 1;
}
------------------------------

------------------------------
#file test.d

extern (C) int get_one();

void main()
{
    printf("Result: %d", get_one());
}
------------------------------

On Linux I compile with:
gcc -c mylib.c

Results in mylib.o

Then compile and link the d code:
dmd test.d mylib.o

This works, no complaints and the output is correct.

When I try to do this with the Microsoft compiler like:
cl /c /TC mylib.c

it produces a mylib.obj

and then I run:
dmd.exe test.d mylib.obj

it complaints with the folowing message:
C:\Documents and Settings\martijn\d>dmd test.obj mylib.obj
c:\opt\dmd\bin\..\..\dm\bin\link.exe test+mylib,,,user32+kernel32/noi;
OPTLINK (R) for Win32  Release 7.50B1
Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

mylib.obj Offset 00000H Record Type 004C
  Error 138: Module or Dictionary corrupt
--- errorlevel 1

Has this someting to do with an import library? How do I get this to work?

Thanks in advance,

Martijn.
Jan 05 2005
parent reply "Charles" <no email.com> writes:
Microsoft uses COFF object format , Digital Mars uses OMF.  Youll need to
compile mylib.c with DMC ( dmc -c mylib.c ) , and then the rest of the
command.

Seems to be a source of many headaches , I would complain but DM is blazing
fast i think OMF is partly a reason :).  It also helps promote DMC so thats
good too.

Charlie

"Tino" <mvandenboogaard gmail.com> wrote in message
news:crhrvl$6vg$1 digitaldaemon.com...
 Hi,

 I know it is possible to call a C function from D. It works on Linux. I
 don't have a very in depth knowledge of C and linking so please excuse
 my ignorence if I have missed something obvious.


 I have the following two files:

 ------------------------------
 #file mylib.c
 int get_one()
 {
     return 1;
 }
 ------------------------------

 ------------------------------
 #file test.d

 extern (C) int get_one();

 void main()
 {
     printf("Result: %d", get_one());
 }
 ------------------------------

 On Linux I compile with:
 gcc -c mylib.c

 Results in mylib.o

 Then compile and link the d code:
 dmd test.d mylib.o

 This works, no complaints and the output is correct.

 When I try to do this with the Microsoft compiler like:
 cl /c /TC mylib.c

 it produces a mylib.obj

 and then I run:
 dmd.exe test.d mylib.obj

 it complaints with the folowing message:
 C:\Documents and Settings\martijn\d>dmd test.obj mylib.obj
 c:\opt\dmd\bin\..\..\dm\bin\link.exe test+mylib,,,user32+kernel32/noi;
 OPTLINK (R) for Win32  Release 7.50B1
 Copyright (C) Digital Mars 1989 - 2001  All Rights Reserved

 mylib.obj Offset 00000H Record Type 004C
   Error 138: Module or Dictionary corrupt
 --- errorlevel 1

 Has this someting to do with an import library? How do I get this to work?

 Thanks in advance,

 Martijn.

Jan 05 2005
parent Tino <mvandenboogaard gmail.com> writes:
Indeed that works! Thanks Charles. Learned something new on this 
beautiful day!


Charles wrote:
 Microsoft uses COFF object format , Digital Mars uses OMF.  Youll need to
 compile mylib.c with DMC ( dmc -c mylib.c ) , and then the rest of the
 command.
 
 Seems to be a source of many headaches , I would complain but DM is blazing
 fast i think OMF is partly a reason :).  It also helps promote DMC so thats
 good too.
 
 Charlie

Jan 05 2005