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 3.7.4.1) 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 10.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 |
EXPORTS 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 |