Jump to page: 1 2 3
Thread overview
Linking C libraries with DMD
Jan 21, 2016
jmh530
Jan 21, 2016
W.J.
Jan 21, 2016
Andrea Fontana
Jan 21, 2016
W.J.
Jan 21, 2016
jmh530
Jan 21, 2016
Dibyendu Majumdar
Jan 21, 2016
jmh530
Jan 21, 2016
Dibyendu Majumdar
Jan 21, 2016
jmh530
Jan 21, 2016
Dibyendu Majumdar
Jan 21, 2016
jmh530
Jan 21, 2016
Dibyendu Majumdar
Jan 21, 2016
jmh530
Jan 21, 2016
jmh530
Jan 21, 2016
Dibyendu Majumdar
Jan 21, 2016
jmh530
Jan 22, 2016
W.J.
Jan 22, 2016
jmh530
Jan 22, 2016
bachmeier
Jan 22, 2016
jmh530
Jan 22, 2016
bachmeier
Jan 22, 2016
Mike Parker
Jan 22, 2016
jmh530
Jan 22, 2016
Mike Parker
Jan 22, 2016
jmh530
Feb 02, 2016
jmh530
Feb 02, 2016
jmh530
January 21, 2016
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.
January 21, 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.
January 21, 2016
On Thursday, 21 January 2016 at 16:57:26 UTC, W.J. wrote:
> You need to port the header file to d. i believe there's the htod utility, however I haven't used that yet.

You should try with dstep too.

More info here:
http://wiki.dlang.org/List_of_Bindings

And here:
http://wiki.dlang.org/D_binding_for_C
January 21, 2016
On Thursday, 21 January 2016 at 17:00:14 UTC, Andrea Fontana wrote:
> On Thursday, 21 January 2016 at 16:57:26 UTC, W.J. wrote:
>> You need to port the header file to d. i believe there's the htod utility, however I haven't used that yet.
>
> You should try with dstep too.
>
> More info here:
> http://wiki.dlang.org/List_of_Bindings
>
> And here:
> http://wiki.dlang.org/D_binding_for_C

Interesting read. Thanks for sharing!

January 21, 2016
On Thursday, 21 January 2016 at 16:57:26 UTC, W.J. wrote:
>
> 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.

I already ported the header file to d. What I can't get to work is linking a dynamic library.

The whole -L-L is definitely not intuitive. The optlink documentation doesn't even describe a -L option. Anyway, it doesn't give an error when I use a plus so I tried
dmd <file.d> -L-L+C:\folder\
and it still isn't picking it up. I figured I needed to tell the linker what the file actually is, so I tried
dmd <file.d> -L-L+C:\folder\ -L-lib+<libfile.lib>
and that (and a variety of variations) gives errors that LIB isn't recognized.
January 21, 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).

Hi I am also new to D and trying to do similar things - i.e. call a shared library written in C from D, but also create a shared library in D.

For the latter - on Windows 10 b64-bit - I am using following options for example:

-shared -L/LIBPATH:c:\\lib  -L//IMPLIB:mylib.lib

In my case I would like stuff from my D code to be exported. I found that I need to do following if I want to export a C API.

extern (C) export void myfunc();

I did not find examples of how to export D classes / functions - and right now I am getting link errors when trying to export D code.

Regards
Dibyendu
January 21, 2016
On Thursday, 21 January 2016 at 21:39:08 UTC, Dibyendu Majumdar wrote:
>
> Hi I am also new to D and trying to do similar things - i.e. call a shared library written in C from D, but also create a shared library in D.
>
> For the latter - on Windows 10 b64-bit - I am using following options for example:
>
> -shared -L/LIBPATH:c:\\lib  -L//IMPLIB:mylib.lib
>
> In my case I would like stuff from my D code to be exported. I found that I need to do following if I want to export a C API.
>
> extern (C) export void myfunc();
>
> I did not find examples of how to export D classes / functions - and right now I am getting link errors when trying to export D code.
>
> Regards
> Dibyendu

I'm not having any luck using your options with dmd either (excluding -shared because I don't need to create a shared D library).
January 21, 2016
On Thursday, 21 January 2016 at 21:55:10 UTC, jmh530 wrote:
>>
>> For the latter - on Windows 10 b64-bit - I am using following options for example:
>>
>> -shared -L/LIBPATH:c:\\lib  -L//IMPLIB:mylib.lib
>>
>
> I'm not having any luck using your options with dmd either (excluding -shared because I don't need to create a shared D library).

Sorry forgot to mention that I also include the library I am linking to. Example:

dmd -m64 prog.d -L/LIBPATH:c:\lib -Lyourlib.lib

Where yourlib.lib and yourlib.dll are in c:\lib folder.
January 21, 2016
On Thursday, 21 January 2016 at 22:02:57 UTC, Dibyendu Majumdar wrote:
> On Thursday, 21 January 2016 at 21:55:10 UTC, jmh530 wrote:
>>>
>>> For the latter - on Windows 10 b64-bit - I am using following options for example:
>>>
>>> -shared -L/LIBPATH:c:\\lib  -L//IMPLIB:mylib.lib
>>>
>>
>> I'm not having any luck using your options with dmd either (excluding -shared because I don't need to create a shared D library).
>
> Sorry forgot to mention that I also include the library I am linking to. Example:
>
> dmd -m64 prog.d -L/LIBPATH:c:\lib -Lyourlib.lib
>
> Where yourlib.lib and yourlib.dll are in c:\lib folder.

The -L/LIBPATH:c:\lib gives me an error that
OPTLINK : Warning 9: Unknown Option : LIBPATH
and then gives the path I put is not found.

At least when it's outputting the text, it's combining
:C:\lib\yourlib.lib
so it seemingly is finding it.
January 21, 2016
On Thursday, 21 January 2016 at 22:09:47 UTC, jmh530 wrote:
> The -L/LIBPATH:c:\lib gives me an error that
> OPTLINK : Warning 9: Unknown Option : LIBPATH
> and then gives the path I put is not found.
>
> At least when it's outputting the text, it's combining
> :C:\lib\yourlib.lib
> so it seemingly is finding it.

OPTLINK is for 32-bit code - the options I showed are for 64-bit, which uses MS LINK. You get 64-bit code by adding -m64.

Regards
« First   ‹ Prev
1 2 3