Thread overview
linking to C libraries
Dec 05, 2007
aeschere
Dec 05, 2007
Bill Baxter
Dec 05, 2007
Robert Fraser
December 05, 2007
I am trying to use D in more of my everyday programming (utility programming at the moment).  However, quite often I need to link to various C libraries (for example pcap).  Does D support linking to C libraries?  If it does, are there special concerns that I need to be aware of?

Thank you in advance for your time,

Michael
December 05, 2007
aeschere wrote:
> I am trying to use D in more of my everyday programming (utility
> programming at the moment).  However, quite often I need to link to
> various C libraries (for example pcap).  Does D support linking to C
> libraries?  If it does, are there special concerns that I need to be
> aware of?
> 
> Thank you in advance for your time,

D does support linking to C.  The main caveat is that if you're using the DMD compiler on Windows you will need to compile the C library with DMC (the Digital Mars C compiler).  This is because DMD/DMC only support compiled code and libraries in the OMF format, whereas Visual Studio and MinGW generate COFF.

The other option is if the C code is in a DLL on Windows, then you can create a DMD-compatible import lib from it using the 'implib' program from the free "Basic Utilities Package" (BUP):
* http://www.digitalmars.com/ctg/implib.html
* http://ftp.digitalmars.com/bup.zip


On linux DMD uses ELF like other linux compilers, so I think it's easier there.

There's a whole different set of answers to this question if you're using GDC.  This sounds like a good topic for a FAQ page somewhere on the wiki.

--bb
December 05, 2007
aeschere wrote:
> I am trying to use D in more of my everyday programming (utility
> programming at the moment).  However, quite often I need to link to
> various C libraries (for example pcap).  Does D support linking to C
> libraries?  If it does, are there special concerns that I need to be
> aware of?
> 
> Thank you in advance for your time,
> 
> Michael
Yes, that's one of the main features of D :-)!

Check out the docs for it: http://www.digitalmars.com/d/interfaceToC.html

I'd recommend taking the C header files and running them through a program that will automatically translate them to D modules:

http://www.digitalmars.com/d/htod.html

That page explains some of the syntactic differences and how to get it all going smoothly.