1 <Chapter id="win32-dlls">
2 <Title>Building and using Win32 DLLs
6 <IndexTerm><Primary>Dynamic link libraries, Win32</Primary></IndexTerm>
7 <IndexTerm><Primary>DLLs, Win32</Primary></IndexTerm>
8 On Win32 platforms, the compiler is capable of both producing and using
9 dynamic link libraries (DLLs) containing ghc-compiled code. This
10 section shows you how to make use of this facility.
13 <Sect1 id="win32-dlls-link">
14 <Title>Linking with DLLs
18 The default on Win32 platforms is to link applications in such a way
19 that the executables will use the Prelude and system libraries DLLs,
20 rather than contain (large chunks of) them. This is transparent at the
29 main = putStrLn "hello, world!"
30 sh$ ghc -o main main.hs
31 ghc: module version changed to 1; reason: no old .hi file
34 -rwxr-xr-x 1 544 everyone 6144 May 3 17:11 main.exe*
43 will give you a binary as before, but the <Filename>main.exe</Filename> generated will use the Prelude and RTS DLLs instead.
47 6K for a <Literal>"hello, world"</Literal> application---not bad, huh? :-)
52 <Sect1 id="win32-dlls-linking-static">
53 <Title>Not linking with DLLs
54 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
57 If you want to build an executable that doesn't depend on any
58 ghc-compiled DLLs, use the <Option>-static</Option> option to link in
63 Notice that you cannot mix code that has been compiled with
64 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
65 option on all the Haskell modules that make up your application.
70 <Sect1 id="win32-dlls-create">
75 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
76 <IndexTerm><Primary>--mk-dll</Primary></IndexTerm>
77 Sealing up your Haskell library inside a DLL is quite straightforward;
78 compile up the object files that make up the library, and then build
79 the DLL by issuing the following command:
84 ghc --mk-dll -o HSsuper.dll A.o Super.o B.o libmine.a -lgdi32
89 By feeding the ghc compiler driver the option <Option>--mk-dll</Option>, it
90 will build a DLL rather than produce an executable. The DLL will
91 consist of all the object files and archives given on the command
96 A couple of things to notice:
106 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
107 to use <Option>-package-name dll-name</Option> when compiling modules that
108 belong to a DLL. If you don't, Haskell code that calls entry points in that
109 DLL will do so incorrectly, and a crash will result.
116 By default, the entry points of all the object files will
117 be exported from the DLL when using <Option>--mk-dll</Option>. Should you want to constrain this, you can specify the <Emphasis>module definition file</Emphasis> to use on the command line as follows:
120 ghc --mk-dll -o .... -optdll--def -optdllMyDef.def
123 See Microsoft documentation for details, but a module definition file
124 simply lists what entry points you want to export. Here's one that's
125 suitable when building a Haskell COM server DLL:
129 DllCanUnloadNow = DllCanUnloadNow@0
130 DllGetClassObject = DllGetClassObject@12
131 DllRegisterServer = DllRegisterServer@0
132 DllUnregisterServer = DllUnregisterServer@0
141 In addition to creating a DLL, the <Option>--mk-dll</Option> option will also
142 create an import library. The import library name is derived from the
143 name of the DLL, as follows:
146 DLL: HScool.dll ==> import lib: libHScool_imp.a
150 The naming scheme may look a bit weird, but it has the purpose of
151 allowing the co-existence of import libraries with ordinary static
152 libraries (e.g., <Filename>libHSfoo.a</Filename> and <Filename>libHSfoo_imp.a</Filename>.
154 Additionally, when the compiler driver is linking in non-static mode,
155 it will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
156 <Option>-lHSfoo_imp</Option>. By doing this for you, switching from non-static
157 to static linking is simply a question of adding <Option>-static</Option> to
172 ;;; Local Variables: ***
174 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***