www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - linking with C libraries

reply Aureliano <gazer pisem.net> writes:
Hi, everyone!
I am starting to use D, and I am very excited about it.

It's said that D program can call C functions directly. That's great, and I
understand how to convert *.h into *.d. My question is: What kinds of C library
files can be used (in linking stage)? I did not find the answer in D's HTML
docs. dmd's link.exe does not accept my C libraries with message
" Error 43: Not a Valid Library File"

More specific. Suppose I have a C library with source, and I want to use it in
my D project. The library is big and complex enough and has unique
functionality, which I don't want to port into D myself. (I am now talking about
FreeType). Which compiler and linker (for dll) I should use to produce .lib
which I can link to my D program? I use DJGPP, MinGW, Cygwin, MSVC, Intel C++,
and Metrowerks, all under Windows. (And FreeType works just fine with all of
them in my C/C++ programs).

Thanks a lot!

Aura
Aug 16 2004
next sibling parent reply anon <anon_member pathlink.com> writes:
In article <cfs6ku$onq$1 digitaldaemon.com>, Aureliano says...
Hi, everyone!
I am starting to use D, and I am very excited about it.

It's said that D program can call C functions directly. That's great, and I
understand how to convert *.h into *.d. My question is: What kinds of C library
files can be used (in linking stage)? I did not find the answer in D's HTML
docs. dmd's link.exe does not accept my C libraries with message
" Error 43: Not a Valid Library File"

More specific. Suppose I have a C library with source, and I want to use it in
my D project. The library is big and complex enough and has unique
functionality, which I don't want to port into D myself. (I am now talking about
FreeType). Which compiler and linker (for dll) I should use to produce .lib
which I can link to my D program? I use DJGPP, MinGW, Cygwin, MSVC, Intel C++,
and Metrowerks, all under Windows. (And FreeType works just fine with all of
them in my C/C++ programs).

Thanks a lot!

Aura

convert your .h files to .d. extern (C): void c_lib_function(/* arguments */); // other functions Then, you can use them in your D file like normal. Use a command similar to this to compile your D program: dmd d_file.d your_c_lib.lib OK? If you're .lib file doesn't work... then I don't know what to do!
Aug 17 2004
parent reply Aureliano <gazer pisem.net> writes:
In article <cfsb7d$r2l$1 digitaldaemon.com>, anon says...

I hope this helps:
convert your .h files to .d.

extern (C):
void c_lib_function(/* arguments */);
// other functions

Then, you can use them in your D file like normal.
Use a command similar to this to compile your D program:

dmd d_file.d your_c_lib.lib

OK? If you're .lib file doesn't work... then I don't know what to do!

The .lib does not work. There should be some specified way to produce C .libs, linkable by D's link.exe. When I try to supply .lib, produced by MinGW's 'ar.exe' or by MSVC's 'lib.exe', DMD's link.exe does not accept it. Thanks for answering, anyway! Aura
Aug 17 2004
next sibling parent kinghajj <kinghajj_member pathlink.com> writes:
In article <cfslti$v7l$1 digitaldaemon.com>, Aureliano says...
In article <cfsb7d$r2l$1 digitaldaemon.com>, anon says...

I hope this helps:
convert your .h files to .d.

extern (C):
void c_lib_function(/* arguments */);
// other functions

Then, you can use them in your D file like normal.
Use a command similar to this to compile your D program:

dmd d_file.d your_c_lib.lib

OK? If you're .lib file doesn't work... then I don't know what to do!

The .lib does not work. There should be some specified way to produce C .libs, linkable by D's link.exe. When I try to supply .lib, produced by MinGW's 'ar.exe' or by MSVC's 'lib.exe', DMD's link.exe does not accept it. Thanks for answering, anyway! Aura

Very simple: lib -c my.lib my.objs ... You can make C & D libraries with it.
Aug 17 2004
prev sibling parent reply "Walter" <newshound digitalmars.com> writes:
"Aureliano" <gazer pisem.net> wrote in message
news:cfslti$v7l$1 digitaldaemon.com...
 In article <cfsb7d$r2l$1 digitaldaemon.com>, anon says...

I hope this helps:
convert your .h files to .d.

extern (C):
void c_lib_function(/* arguments */);
// other functions

Then, you can use them in your D file like normal.
Use a command similar to this to compile your D program:

dmd d_file.d your_c_lib.lib

OK? If you're .lib file doesn't work... then I don't know what to do!

The .lib does not work. There should be some specified way to produce C

 linkable by D's link.exe. When I try to supply .lib, produced by MinGW's
 'ar.exe' or by MSVC's 'lib.exe', DMD's link.exe does not accept it.
 Thanks for answering, anyway!

The problem you're having has nothing to do with C vs D compatibility, it is because the library you're using is not in OMF format. You could try COFF2OMF to convert it. If you can get the library to link with DMC, it will link with D.
Aug 17 2004
parent Aureliano <gazer pisem.net> writes:
Thanks Ilya, Walter! Now it works.

I compiled FreeType with DMC and it works OK. (Thanks to FreeType's code
quality). It seems to work a bit slower than compiled by GCC or MSVC, but
perhaps I missed some compiler optimization options (I used only -o). Anyway it
works fine. Converting FreeType headers into D modules will be another task of
course.

Aura



Ilya Minkov says...

You are probably trying to link with .LIB files from the wrong C 
compiler. DigitalMars D uses the same back-end and binary tools as 
DigitalMars C and C++, so these tools have to be used, because the file 
format differs severely from that of Microsoft and MinGW.

Please refer to DMC Compiler and Tools guide from the DigitalMars 
website on help how to create stub .LIBs from DLLs using implib (basic 
tools package). If you want a static library, recompile it with DMC, or 
you can try the commercial advanced utilities package COFF2OMF.

Walter says...
The problem you're having has nothing to do with C vs D compatibility, it is
because the library you're using is not in OMF format. You could try
COFF2OMF to convert it. If you can get the library to link with DMC, it will
link with D.

Aug 17 2004
prev sibling parent Ilya Minkov <minkov cs.tum.edu> writes:
You are probably trying to link with .LIB files from the wrong C 
compiler. DigitalMars D uses the same back-end and binary tools as 
DigitalMars C and C++, so these tools have to be used, because the file 
format differs severely from that of Microsoft and MinGW.

Please refer to DMC Compiler and Tools guide from the DigitalMars 
website on help how to create stub .LIBs from DLLs using implib (basic 
tools package). If you want a static library, recompile it with DMC, or 
you can try the commercial advanced utilities package COFF2OMF.

-eye

Aureliano schrieb:

 Hi, everyone!
 I am starting to use D, and I am very excited about it.
 
 It's said that D program can call C functions directly. That's great, and I
 understand how to convert *.h into *.d. My question is: What kinds of C library
 files can be used (in linking stage)? I did not find the answer in D's HTML
 docs. dmd's link.exe does not accept my C libraries with message
 " Error 43: Not a Valid Library File"
 
 More specific. Suppose I have a C library with source, and I want to use it in
 my D project. The library is big and complex enough and has unique
 functionality, which I don't want to port into D myself. (I am now talking
about
 FreeType). Which compiler and linker (for dll) I should use to produce .lib
 which I can link to my D program? I use DJGPP, MinGW, Cygwin, MSVC, Intel C++,
 and Metrowerks, all under Windows. (And FreeType works just fine with all of
 them in my C/C++ programs).
 
 Thanks a lot!
 
 Aura
 
 

Aug 17 2004