Linking C libraries with DMD
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jan 21 20:03:27 PST 2016
On Friday, 22 January 2016 at 02:39:33 UTC, jmh530 wrote:
>
> The LearningD book says that you should compile the libraries
> with DMC on Windows, but I can't figure out how to generate a
> shared library on DMC. I didn't get the implib error for what I
> was working on before.
>
> I feel like getting stuff to work with Windows is always such a
> hassle, but that's the only way I'll be able to use this stuff
> at work.
Your confusion appears to be coming from a lack of understanding
of what's going on under the hood. When working with a system
language like D, it is imperative to understand what the compiler
and linker are doing. The same issues you are having can arise
when using C and C++, they are just less common as you tend to
use the same compiler toolchain for both your executable and your
libraries.
First of all, understand that DMD does not use just one linker on
Windows. The default is OPTLINK, which only works with 32-bit
object files (and by extension, library files, as they are just
archives of objects) in the OMF format. When compiling with
-m32mscoff or -m64, DMD uses the Microsoft linker, which deals
with objects in the COFF format. This matters at *link time*, not
at runtime. So it *generally* (see below) doesn't matter which
format your DLL is in, as it is loaded at runtime no matter how
you compile.
Second, understand that when you choose to link with an import
library rather than loading the DLL manually, then it is the
format of the import library that's important. It needs to be in
the OMF format if you are compiling with vanilla DMD and in the
COFF format if not. OMF import libraries can be generated from
COFF DLLS with implib. Import libraries generated by the MinGW
toolchain are actually in the COFF format, but they are not
always compatible with the Microsoft toolchain. You are likely
going to have issues even when compiling with -m32mscoff or -m64.
Your implib difficulties may actually be arising because the DLL
was compiled with MinGW, despite it being in COFF.
Third, understand that passing -L to DMD tells it that the
succeeding flag should be passed to the linker. On Windows, -L-L
has no meaning, as neither OPTLINK nor the MS linker accept the
-L switch. -L is used with GCC to specify the library path, so in
the command line -L-L/path/to/libs, the first -L tells DMD that
the next part is for the linker and the second -L tells the
linker where to find libraries. Again, this is only for the GCC
toolchain. For DMD on Windows, how you specify the library path
depends on whether you are linking with OPTLINK or the MS linker.
As for the libraries themselves, you don't need to don't actually
need the -L flag on Windows. In fact, you can save yourself some
trouble and just pass the full path to any libraries you need
with no flags at all:
dmd myapp.d C:\path\to\libs\mylib.lib
As long as the library is in the appropriate format, this command
line will do the right thing.
I strongly recommend that you compile your DLL and generate the
import library with the Microsoft tools. Then you should be able
to use the 32-bit version with -m32mscoff and the 64-bit version
with -m64. This should /just work/.
Development on Windows is not any more difficult than on Linux.
It's annoying, sure, but not difficult. You just need to make
sure that all of the tools you are using are compatible.
More information about the Digitalmars-d-learn
mailing list