fd096e5be97ed25b10c9f7e068f578cda9080bf7
[ghc-hetmet.git] / ghc / docs / users_guide / win32-dlls.sgml
1 <Chapter id="win32-dlls">
2 <Title>Building and using Win32 DLLs
3 </Title>
4
5 <Para>
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.
11 </Para>
12
13 <Para>
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).
18 </Para>
19
20
21 <Sect1 id="win32-dlls-link">
22 <Title>Linking with DLLs
23 </Title>
24
25 <Para>
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
29 command-line, so 
30 </Para>
31
32 <Para>
33 <Screen>
34 sh$ cat main.hs
35 module Main where
36 main = putStrLn "hello, world!"
37 sh$ ghc -o main main.hs
38 ghc: module version changed to 1; reason: no old .hi file
39 sh$ strip main.exe
40 sh$ ls -l main.exe
41 -rwxr-xr-x   1 544      everyone     4608 May  3 17:11 main.exe*
42 sh$ ./main
43 hello, world!
44 sh$ 
45 </Screen>
46 </Para>
47
48 <Para>
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
51 statically.
52 </Para>
53
54 <Para>
55 4K for a <Literal>"hello, world"</Literal> application---not bad, huh? :-)
56 </Para>
57
58 </Sect1>
59
60 <Sect1 id="win32-dlls-linking-static">
61 <Title>Not linking with DLLs
62 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
63
64 <Para>
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
67 the code statically.
68 </Para>
69
70 <Para>
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.
74 </Para>
75
76 </Sect1>
77
78 <Sect1 id="win32-dlls-create">
79 <Title>Creating a DLL
80 </Title>
81
82 <Para>
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:
93 </Para>
94
95 <Para>
96 <Screen>
97 ghc --mk-dll -o foo.dll bar.o baz.o wibble.a -lfooble
98 </Screen>
99 </Para>
100
101 <Para>
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
105 line.
106 </Para>
107
108 <Para>
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
111 building the DLL.
112 </Para>
113
114 <Para>
115 A couple of things to notice:
116 </Para>
117
118 <Para>
119
120 <ItemizedList>
121 <ListItem>
122 <Para>
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.
132 </Para>
133 </ListItem>
134
135 <ListItem>
136 <Para>
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:
141
142 <Screen>
143 ghc --mk-dll -o .... -optdll--def -optdllMyDef.def
144 </Screen>
145
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:
149
150 <ProgramListing>
151 EXPORTS
152  DllCanUnloadNow     = DllCanUnloadNow@0
153  DllGetClassObject   = DllGetClassObject@12
154  DllRegisterServer   = DllRegisterServer@0
155  DllUnregisterServer = DllUnregisterServer@0
156 </ProgramListing>
157 </Para>
158 </ListItem>
159
160 <ListItem>
161 <Para>
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:
165
166 <ProgramListing>
167 DLL: HScool.dll  ==&#62; import lib: libHScool_imp.a
168 </ProgramListing>
169
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&lowbar;imp.a</Filename>.
174
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&lowbar;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.
180
181 </Para>
182 </ListItem>
183 </ItemizedList>
184 </Para>
185
186 </Sect1>
187
188
189 <Sect1 id="win32-dlls-foreign">
190 <Title>Making DLLs to be called from other languages</Title>
191
192 <Para>
193
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:
199
200 </Para>
201
202 <ItemizedList>
203
204 <ListItem>
205 <Para>
206 Use <Literal>foreign export</Literal> declarations to export the Haskell
207 functions you want to call from the outside. For example,
208
209 <ProgramListing>
210 module Adder where
211
212 adder :: Int -> Int -> IO Int  -- gratuitous use of IO
213 adder x y = return (x+y)
214
215 foreign export stdcall adder :: Int -> Int -> IO Int
216 </ProgramListing>
217 </Para>
218 </ListItem>
219
220 <ListItem>
221 <Para>
222 Compile it up:
223
224 <Screen>
225 ghc -c adder.hs -fglasgow-exts
226 </Screen>
227   
228 This will produce two files, adder.o and adder_stub.o
229 </Para>
230 </ListItem>
231
232 <ListItem>
233 <Para>
234 compile up a <Function>DllMain()</Function> that starts up the Haskell
235 RTS---a possible implementation is:
236
237 <ProgramListing>
238 #include &lt;windows.h&gt;
239 #include &lt;Rts.h&gt;
240
241 EXTFUN(__init_Adder);
242
243 static char* args[] = { "ghcDll", NULL };
244                        /* N.B. argv arrays must end with NULL */
245 BOOL
246 STDCALL
247 DllMain
248    ( HANDLE hModule
249    , DWORD reason
250    , void* reserved
251    )
252 {
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);
256       return TRUE;
257   }
258   return TRUE;
259 }
260 </ProgramListing>
261
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).
265
266 Compile this up:
267
268 <Screen>
269 gcc -c dllMain.c
270 </Screen>
271 </Para>
272 </ListItem>
273
274 <ListItem>
275 <Para>
276 Construct the DLL:
277
278 <Screen>
279 ghc --mk-dll -o adder.dll adder.o adder_stub.o dllMain.o
280 </Screen>
281
282 </Para>
283 </ListItem>
284
285 <ListItem>
286 <Para>
287 Start using <Function>adder</Function> from VBA---here's how I would
288 <Constant>Declare</Constant> it:
289
290 <ProgramListing>
291 Private Declare Function adder Lib "adder.dll" Alias "adder@8"
292       (ByVal x As Long, ByVal y As Long) As Long
293 </ProgramListing>
294
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.
297 </Para>
298
299 <Para>
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.
303 </Para>
304
305 </ListItem>
306
307 </ItemizedList>
308
309 </Sect1>
310
311 </Chapter>
312
313 <!-- Emacs stuff:
314      ;;; Local Variables: ***
315      ;;; mode: sgml ***
316      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
317      ;;; End: ***
318  -->