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.
14 Until recently, <Command>strip</Command> didn't work reliably on DLLs, so you
15 should test your version with care, or make sure you have the latest
16 binutils. Unfortunately, we don't know exactly which version of binutils
17 cured the problem (it was supposedly fixed some years ago).
21 <Sect1 id="win32-dlls-link">
22 <Title>Linking with DLLs
26 The default on Win32 platforms is to link applications in such a way
27 that the executables will use the Prelude and system libraries DLLs,
28 rather than contain (large chunks of) them. This is transparent at the
36 main = putStrLn "hello, world!"
37 sh$ ghc -o main main.hs
38 ghc: module version changed to 1; reason: no old .hi file
41 -rwxr-xr-x 1 544 everyone 4608 May 3 17:11 main.exe*
49 will give you a binary as before, but the <Filename>main.exe</Filename>
50 generated will use the Prelude and RTS DLLs instead of linking them in
55 4K for a <Literal>"hello, world"</Literal> application---not bad, huh? :-)
60 <Sect1 id="win32-dlls-linking-static">
61 <Title>Not linking with DLLs
62 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
65 If you want to build an executable that doesn't depend on any
66 ghc-compiled DLLs, use the <Option>-static</Option> option to link in
71 Notice that you cannot mix code that has been compiled with
72 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
73 option on all the Haskell modules that make up your application.
78 <Sect1 id="win32-dlls-create">
83 <emphasis>Making libraries into DLLs doesn't work on Windows at the
84 moment (and is no longer supported); however, all the machinery is
85 still there. If you're interested, contact the GHC team. Note that
86 building an entire Haskell application as a DLL is still supported
87 (it's just inter-DLL Haskell calls that don't work).</emphasis>
88 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
89 <IndexTerm><Primary>--mk-dll</Primary></IndexTerm>
90 Sealing up your Haskell library inside a DLL is straightforward;
91 compile up the object files that make up the library, and then build
92 the DLL by issuing a command of the form:
97 ghc --mk-dll -o foo.dll bar.o baz.o wibble.a -lfooble
102 By feeding the ghc compiler driver the option <Option>--mk-dll</Option>, it
103 will build a DLL rather than produce an executable. The DLL will
104 consist of all the object files and archives given on the command
109 To create a `static' DLL, i.e. one that does not depend on the GHC DLLs,
110 use the <Option>-static</Option> when compiling up your Haskell code and
115 A couple of things to notice:
123 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
124 to use <Option>-package-name dll-name</Option> when compiling modules that
125 belong to a DLL if you're going to call them from Haskell. Otherwise, Haskell
126 code that calls entry points in that DLL will do so incorrectly, and crash.
127 For similar reasons, you can only compile a single module tree into a DLL,
128 as <Function>startupHaskell</Function> needs to be able to call its
129 initialisation function, and only takes one such argument (see <XRef
130 LinkEnd="win32-dlls-foreign">). Hence the modules
131 you compile into a DLL must have a common root.
137 By default, the entry points of all the object files will be exported from
138 the DLL when using <Option>--mk-dll</Option>. Should you want to constrain
139 this, you can specify the <Emphasis>module definition file</Emphasis> to use
140 on the command line as follows:
143 ghc --mk-dll -o .... -optdll--def -optdllMyDef.def
146 See Microsoft documentation for details, but a module definition file
147 simply lists what entry points you want to export. Here's one that's
148 suitable when building a Haskell COM server DLL:
152 DllCanUnloadNow = DllCanUnloadNow@0
153 DllGetClassObject = DllGetClassObject@12
154 DllRegisterServer = DllRegisterServer@0
155 DllUnregisterServer = DllUnregisterServer@0
162 In addition to creating a DLL, the <Option>--mk-dll</Option> option also
163 creates an import library. The import library name is derived from the
164 name of the DLL, as follows:
167 DLL: HScool.dll ==> import lib: libHScool_imp.a
170 The naming scheme may look a bit weird, but it has the purpose of allowing
171 the co-existence of import libraries with ordinary static libraries (e.g.,
172 <Filename>libHSfoo.a</Filename> and
173 <Filename>libHSfoo_imp.a</Filename>.
175 Additionally, when the compiler driver is linking in non-static mode, it
176 will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
177 <Option>-lHSfoo_imp</Option>. By doing this for you, switching from
178 non-static to static linking is simply a question of adding
179 <Option>-static</Option> to your command line.
189 <Sect1 id="win32-dlls-foreign">
190 <Title>Making DLLs to be called from other languages</Title>
194 If you want to package up Haskell code to be called from other languages,
195 such as Visual Basic or C++, there are some extra things it is useful to
196 know. The dirty details are in the <Emphasis>Foreign Function
197 Interface</Emphasis> definition, but it can be tricky to work out how to
198 combine this with DLL building, so here's an example:
206 Use <Literal>foreign export</Literal> declarations to export the Haskell
207 functions you want to call from the outside. For example,
212 adder :: Int -> Int -> IO Int -- gratuitous use of IO
213 adder x y = return (x+y)
215 foreign export stdcall adder :: Int -> Int -> IO Int
225 ghc -c adder.hs -fglasgow-exts
228 This will produce two files, adder.o and adder_stub.o
234 compile up a <Function>DllMain()</Function> that starts up the Haskell
235 RTS---a possible implementation is:
238 #include <windows.h>
239 #include <Rts.h>
241 EXTFUN(__init_Adder);
243 static char* args[] = { "ghcDll", NULL };
244 /* N.B. argv arrays must end with NULL */
253 if (reason == DLL_PROCESS_ATTACH) {
254 /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
255 startupHaskell(1, args, __init_Adder);
262 Here, <Literal>Adder</Literal> is the name of the root module in the module
263 tree (as mentioned above, there must be a single root module, and hence a
264 single module tree in the DLL).
279 ghc --mk-dll -o adder.dll adder.o adder_stub.o dllMain.o
287 Start using <Function>adder</Function> from VBA---here's how I would
288 <Constant>Declare</Constant> it:
291 Private Declare Function adder Lib "adder.dll" Alias "adder@8"
292 (ByVal x As Long, ByVal y As Long) As Long
295 Since this Haskell DLL depends on a couple of the DLLs that come with GHC,
296 make sure that they are in scope/visible.
300 Building statically linked DLLs is the same as in the previous section: it
301 suffices to add <Option>-static</Option> to the commands used to compile up
302 the Haskell source and build the DLL.
314 ;;; Local Variables: ***
316 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***