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 This is no longer supported, and probably doesn't work; but if you want to build an executable that doesn't depend on any ghc-compiled DLLs, use the <Option>-static</Option> option to link in the code statically.
61 Notice that you cannot mix code that has been compiled with
62 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
63 option on all the Haskell modules that make up your application.
68 <Sect1 id="win32-dlls-create">
73 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
74 <IndexTerm><Primary>--mk-dll</Primary></IndexTerm>
75 Sealing up your Haskell library inside a DLL is quite straightforward;
76 compile up the object files that make up the library, and then build
77 the DLL by issuing the following command:
82 ghc --mk-dll -o HSsuper.dll A.o Super.o B.o libmine.a -lgdi32
87 By feeding the ghc compiler driver the option <Option>--mk-dll</Option>, it
88 will build a DLL rather than produce an executable. The DLL will
89 consist of all the object files and archives given on the command
94 A couple of things to notice:
104 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
105 to use <Option>-package-name dll-name</Option> when compiling modules that
106 belong to a DLL. If you don't, Haskell code that calls entry points in that
107 DLL will do so incorrectly, and a crash will result.
114 By default, the entry points of all the object files will
115 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:
118 ghc --mk-dll -o .... -optdll--def -optdllMyDef.def
121 See Microsoft documentation for details, but a module definition file
122 simply lists what entry points you want to export. Here's one that's
123 suitable when building a Haskell COM server DLL:
127 DllCanUnloadNow = DllCanUnloadNow@0
128 DllGetClassObject = DllGetClassObject@12
129 DllRegisterServer = DllRegisterServer@0
130 DllUnregisterServer = DllUnregisterServer@0
139 In addition to creating a DLL, the <Option>--mk-dll</Option> option will also
140 create an import library. The import library name is derived from the
141 name of the DLL, as follows:
144 DLL: HScool.dll ==> import lib: libHScool_imp.a
148 The naming scheme may look a bit weird, but it has the purpose of
149 allowing the co-existence of import libraries with ordinary static
150 libraries (e.g., <Filename>libHSfoo.a</Filename> and <Filename>libHSfoo_imp.a</Filename>.
152 Additionally, when the compiler driver is linking in non-static mode,
153 it will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
154 <Option>-lHSfoo_imp</Option>. By doing this for you, switching from non-static
155 to static linking is simply a question of adding <Option>-static</Option> to
170 ;;; Local Variables: ***
172 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***