NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.64 ">

11.3. Creating a DLL

Making libraries into DLLs doesn't work on Windows at the moment (and is no longer supported); however, all the machinery is still there. If you're interested, contact the GHC team. Note that building an entire Haskell application as a DLL is still supported (it's just inter-DLL Haskell calls that don't work). Sealing up your Haskell library inside a DLL is straightforward; compile up the object files that make up the library, and then build the DLL by issuing a command of the form:

ghc ––mk-dll -o foo.dll bar.o baz.o wibble.a -lfooble

By feeding the ghc compiler driver the option ––mk-dll, it will build a DLL rather than produce an executable. The DLL will consist of all the object files and archives given on the command line.

To create a `static' DLL, i.e. one that does not depend on the GHC DLLs, use the -static when compiling up your Haskell code and building the DLL.

A couple of things to notice:

  • Since DLLs correspond to packages (see Section 4.10) you need to use -package-name dll-name when compiling modules that belong to a DLL if you're going to call them from Haskell. Otherwise, Haskell code that calls entry points in that DLL will do so incorrectly, and crash. For similar reasons, you can only compile a single module tree into a DLL, as startupHaskell needs to be able to call its initialisation function, and only takes one such argument (see Section 11.4). Hence the modules you compile into a DLL must have a common root.

  • By default, the entry points of all the object files will be exported from the DLL when using ––mk-dll. Should you want to constrain this, you can specify the module definition file to use on the command line as follows:
    ghc ––mk-dll -o .... -optdll--def -optdllMyDef.def
    See Microsoft documentation for details, but a module definition file simply lists what entry points you want to export. Here's one that's suitable when building a Haskell COM server DLL:
     DllCanUnloadNow     = DllCanUnloadNow@0
     DllGetClassObject   = DllGetClassObject@12
     DllRegisterServer   = DllRegisterServer@0
     DllUnregisterServer = DllUnregisterServer@0

  • In addition to creating a DLL, the ––mk-dll option also creates an import library. The import library name is derived from the name of the DLL, as follows:
    DLL: HScool.dll  ==> import lib: libHScool_imp.a
    The naming scheme may look a bit weird, but it has the purpose of allowing the co-existence of import libraries with ordinary static libraries (e.g., libHSfoo.a and libHSfoo_imp.a. Additionally, when the compiler driver is linking in non-static mode, it will rewrite occurrence of -lHSfoo on the command line to -lHSfoo_imp. By doing this for you, switching from non-static to static linking is simply a question of adding -static to your command line.