2 <title>Running GHC on Win32 systems</title>
6 Starting GHC on Win32 platforms</title>
9 The installer that installs GHC on Win32 also sets up the file-suffix associations
10 for ".hs" and ".lhs" files so that double-clicking them starts <command>ghci</command>.
13 Be aware of that <command>ghc</command> and <command>ghci</command> do
14 require filenames containing spaces to be escaped using quotes:
16 c:\ghc\bin\ghci "c:\\Program Files\\Haskell\\Project.hs"
20 If the quotes are left off in the above command, <command>ghci</command> will
21 interpret the filename as two, "c:\\Program" and "Files\\Haskell\\Project.hs".
24 <!-- not clear whether there are current editions of Win32 OSes that
25 doesn't do this by default.
27 <para> Solution: don't use "Open With...", avoid spaces in file names,
28 or fiddle with the appropriate registry setting:
30 HKEY_CLASSES_ROOT\Unknown\shell\openas\command
32 Notice how the "%1" argument is quoted (or not).
34 <para> This problem doesn't occur when double-clicking.
42 Using GHC (and other GHC-compiled executables) with cygwin</title>
45 <title>Background</title> <para>The cygwin tools aim to provide a
46 unix-style API on top of the windows libraries, to facilitate ports of
47 unix software to windows. To this end, they introduce a unix-style
48 directory hierarchy under some root directory (typically
49 <filename>/</filename> is <filename>C:\cygwin\</filename>). Moreover,
50 everything built against the cygwin API (including the cygwin tools
51 and programs compiled with cygwin's ghc) will see / as the root of
52 their file system, happily pretending to work in a typical unix
53 environment, and finding things like <filename>/bin</filename> and <filename>/usr/include</filename> without
54 ever explicitly bothering with their actual location on the windows
55 system (probably <filename>C:\cygwin\bin</filename> and <filename>C:\cygwin\usr\include</filename>).
59 <sect2><title>The problem</title>
60 <para>GHC, by default, no longer depends on cygwin, but is a native
61 windows program. It is built using mingw, and it uses mingw's ghc
62 while compiling your Haskell sources (even if you call it from
63 cygwin's bash), but what matters here is that - just like any other
64 normal windows program - neither GHC nor the executables it produces
65 are aware of cygwin's pretended unix hierarchy. GHC will happily
66 accept either '/' or '\' as path separators, but it won't know where
67 to find <filename>/home/joe/Main.hs</filename> or <filename>/bin/bash</filename>
68 or the like. This causes all
69 kinds of fun when GHC is used from within cygwin's bash, or in
70 make-sessions running under cygwin.
74 <sect2><title>Things to do</title>
77 <para> Don't use absolute paths in make, configure & co if there is any chance
78 that those might be passed to GHC (or to GHC-compiled programs). Relative
79 paths are fine because cygwin tools are happy with them and GHC accepts
80 '/' as path-separator. And relative paths don't depend on where cygwin's
81 root directory is located, or on which partition or network drive your source
82 tree happens to reside, as long as you 'cd' there first.
86 <para> If you have to use absolute paths (beware of the innocent-looking
87 <literal>ROOT=`pwd`</literal> in makefile hierarchies or configure scripts), cygwin provides
88 a tool called <command>cygpath</command> that can convert cygwin's unix-style paths to their
89 actual windows-style counterparts. Many cygwin tools actually accept
90 absolute windows-style paths (remember, though, that you either need
91 to escape '\' or convert '\' to '/'), so you should be fine just using those
92 everywhere. If you need to use tools that do some kind of path-mangling
93 that depends on unix-style paths (one fun example is trying to interpret ':'
94 as a separator in path lists..), you can still try to convert paths using
95 <command>cygpath</command> just before they are passed to GHC and friends.
99 <para> If you don't have <command>cygpath</command>, you probably don't have cygwin and hence
100 no problems with it... unless you want to write one build process for several
101 platforms. Again, relative paths are your friend, but if you have to use
102 absolute paths, and don't want to use different tools on different platforms,
103 you can simply write a short Haskell program to print the current directory
104 (thanks to George Russell for this idea): compiled with GHC, this will give
105 you the view of the file system that GHC depends on (which will differ
106 depending on whether GHC is compiled with cygwin's gcc or mingw's
107 gcc or on a real unix system..) - that little program can also deal with
108 escaping '\' in paths. Apart from the banner and the startup time,
109 something like this would also do:
111 $ echo "Directory.getCurrentDirectory >>= putStrLn . init . tail . show " | ghci
119 <sect1 id="win32-dlls">
120 <Title>Building and using Win32 DLLs
124 <IndexTerm><Primary>Dynamic link libraries, Win32</Primary></IndexTerm>
125 <IndexTerm><Primary>DLLs, Win32</Primary></IndexTerm>
126 On Win32 platforms, the compiler is capable of both producing and using
127 dynamic link libraries (DLLs) containing ghc-compiled code. This
128 section shows you how to make use of this facility.
132 Until recently, <Command>strip</Command> didn't work reliably on DLLs, so you
133 should test your version with care, or make sure you have the latest
134 binutils. Unfortunately, we don't know exactly which version of binutils
135 cured the problem (it was supposedly fixed some years ago).
139 <Sect2 id="win32-dlls-link">
140 <Title>Linking with DLLs
144 The default on Win32 platforms is to link applications in such a way
145 that the executables will use the Prelude and system libraries DLLs,
146 rather than contain (large chunks of) them. This is transparent at the
154 main = putStrLn "hello, world!"
155 sh$ ghc -o main main.hs
156 ghc: module version changed to 1; reason: no old .hi file
159 -rwxr-xr-x 1 544 everyone 4608 May 3 17:11 main.exe*
167 will give you a binary as before, but the <Filename>main.exe</Filename>
168 generated will use the Prelude and RTS DLLs instead of linking them in
173 4K for a <Literal>"hello, world"</Literal> application—not bad, huh? :-)
178 <Sect2 id="win32-dlls-linking-static">
179 <Title>Not linking with DLLs
180 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
183 If you want to build an executable that doesn't depend on any
184 ghc-compiled DLLs, use the <Option>-static</Option> option to link in
189 Notice that you cannot mix code that has been compiled with
190 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
191 option on all the Haskell modules that make up your application.
196 <Sect2 id="win32-dlls-create">
197 <Title>Creating a DLL
201 <emphasis>Making libraries into DLLs doesn't work on Windows at the
202 moment (and is no longer supported); however, all the machinery is
203 still there. If you're interested, contact the GHC team. Note that
204 building an entire Haskell application as a DLL is still supported
205 (it's just inter-DLL Haskell calls that don't work).</emphasis>
206 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
207 <IndexTerm><Primary>––mk-dll</Primary></IndexTerm>
208 Sealing up your Haskell library inside a DLL is straightforward;
209 compile up the object files that make up the library, and then build
210 the DLL by issuing a command of the form:
215 ghc ––mk-dll -o foo.dll bar.o baz.o wibble.a -lfooble
220 By feeding the ghc compiler driver the option <Option>––mk-dll</Option>, it
221 will build a DLL rather than produce an executable. The DLL will
222 consist of all the object files and archives given on the command
227 To create a `static' DLL, i.e. one that does not depend on the GHC DLLs,
228 use the <Option>-static</Option> when compiling up your Haskell code and
233 A couple of things to notice:
241 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
242 to use <Option>-package-name dll-name</Option> when compiling modules that
243 belong to a DLL if you're going to call them from Haskell. Otherwise, Haskell
244 code that calls entry points in that DLL will do so incorrectly, and crash.
245 For similar reasons, you can only compile a single module tree into a DLL,
246 as <Function>startupHaskell</Function> needs to be able to call its
247 initialisation function, and only takes one such argument (see <XRef
248 LinkEnd="win32-dlls-foreign">). Hence the modules
249 you compile into a DLL must have a common root.
255 By default, the entry points of all the object files will be exported from
256 the DLL when using <Option>––mk-dll</Option>. Should you want to constrain
257 this, you can specify the <Emphasis>module definition file</Emphasis> to use
258 on the command line as follows:
261 ghc ––mk-dll -o .... -optdll--def -optdllMyDef.def
264 See Microsoft documentation for details, but a module definition file
265 simply lists what entry points you want to export. Here's one that's
266 suitable when building a Haskell COM server DLL:
270 DllCanUnloadNow = DllCanUnloadNow@0
271 DllGetClassObject = DllGetClassObject@12
272 DllRegisterServer = DllRegisterServer@0
273 DllUnregisterServer = DllUnregisterServer@0
280 In addition to creating a DLL, the <Option>––mk-dll</Option> option also
281 creates an import library. The import library name is derived from the
282 name of the DLL, as follows:
285 DLL: HScool.dll ==> import lib: libHScool_imp.a
288 The naming scheme may look a bit weird, but it has the purpose of allowing
289 the co-existence of import libraries with ordinary static libraries (e.g.,
290 <Filename>libHSfoo.a</Filename> and
291 <Filename>libHSfoo_imp.a</Filename>.
293 Additionally, when the compiler driver is linking in non-static mode, it
294 will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
295 <Option>-lHSfoo_imp</Option>. By doing this for you, switching from
296 non-static to static linking is simply a question of adding
297 <Option>-static</Option> to your command line.
307 <Sect2 id="win32-dlls-foreign">
308 <Title>Making DLLs to be called from other languages</Title>
312 If you want to package up Haskell code to be called from other languages,
313 such as Visual Basic or C++, there are some extra things it is useful to
314 know. The dirty details are in the <Emphasis>Foreign Function
315 Interface</Emphasis> definition, but it can be tricky to work out how to
316 combine this with DLL building, so here's an example:
324 Use <Literal>foreign export</Literal> declarations to export the Haskell
325 functions you want to call from the outside. For example,
330 adder :: Int -> Int -> IO Int -- gratuitous use of IO
331 adder x y = return (x+y)
333 foreign export stdcall adder :: Int -> Int -> IO Int
343 ghc -c adder.hs -fglasgow-exts
346 This will produce two files, adder.o and adder_stub.o
352 compile up a <Function>DllMain()</Function> that starts up the Haskell
353 RTS-––a possible implementation is:
356 #include <windows.h>
357 #include <Rts.h>
359 EXTFUN(__stginit_Adder);
361 static char* args[] = { "ghcDll", NULL };
362 /* N.B. argv arrays must end with NULL */
371 if (reason == DLL_PROCESS_ATTACH) {
372 /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
373 startupHaskell(1, args, __stginit_Adder);
380 Here, <Literal>Adder</Literal> is the name of the root module in the module
381 tree (as mentioned above, there must be a single root module, and hence a
382 single module tree in the DLL).
397 ghc ––mk-dll -o adder.dll adder.o adder_stub.o dllMain.o
405 Start using <Function>adder</Function> from VBA-––here's how I would
406 <Constant>Declare</Constant> it:
409 Private Declare Function adder Lib "adder.dll" Alias "adder@8"
410 (ByVal x As Long, ByVal y As Long) As Long
413 Since this Haskell DLL depends on a couple of the DLLs that come with GHC,
414 make sure that they are in scope/visible.
418 Building statically linked DLLs is the same as in the previous section: it
419 suffices to add <Option>-static</Option> to the commands used to compile up
420 the Haskell source and build the DLL.
433 ;;; Local Variables: ***
435 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***