Linking C libraries with DMD

W.J. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 21 08:57:26 PST 2016


On Thursday, 21 January 2016 at 16:14:40 UTC, jmh530 wrote:
> I'm trying to understand calling C libraries from D on Windows 
> with DMD. I made a simple example and compiled it with a static 
> library fine (so I've converted the .h file correctly). Then, I 
> compiled with gcc to a shared library (because I cannot for the 
> life of me figure out how to do this with DMC). I then used 
> implib to generate a .lib file (the fact that this is necessary 
> is not described nearly well enough in the documentation).
>
> The next step is getting this to compile with dmd. I'm getting 
> an error that the function isn't getting called, which is 
> suggesting that I'm not linking the .lib file with DMD properly.
>
> The dmd page
> https://dlang.org/dmd-windows.html
> explains that -L is used to pass stuff to the linker. It seems 
> to indicate that this should be the folder that the library is 
> in. The dll and lib files are in the same folder as the .d file 
> I'm trying to compile. So presumably, this should be -L. or 
> -L\. or like -LC:\folder\. But nothing like that works. There's 
> a link on the dmd page to optlink, which doesn't really help me 
> figure this out either. There's also some stuff about set LIB 
> in the sci.ini. Not sure I'm supposed to mess with that.
>
> The LearningD book has some examples as well. I don't have it 
> in front of me right now, but I think I tried what they 
> recommend also. Nevertheless, I feel like the documentation 
> should be clear enough so that this isn't so frustrating.

You need to port the header file to d. i believe there's the htod 
utility, however I haven't used that yet.
Then, basically all you have to do is to tell the linker to link 
against your C .lib.
Remember that -LC:\folder (for dmd) passes "C:\folder" on to the 
linker. Assuming the library folder flag for your linker is -L, 
you'd want to use -L-LC:\folder.


More information about the Digitalmars-d-learn mailing list