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 <Command>strip</Command> seems not to work reliably on DLLs, so it's probably best not to.
18 <Sect1 id="win32-dlls-link">
19 <Title>Linking with DLLs
23 The default on Win32 platforms is to link applications in such a way
24 that the executables will use the Prelude and system libraries DLLs,
25 rather than contain (large chunks of) them. This is transparent at the
34 main = putStrLn "hello, world!"
35 sh$ ghc -o main main.hs
36 ghc: module version changed to 1; reason: no old .hi file
39 -rwxr-xr-x 1 544 everyone 6144 May 3 17:11 main.exe*
48 will give you a binary as before, but the <Filename>main.exe</Filename> generated will use the Prelude and RTS DLLs instead.
52 6K for a <Literal>"hello, world"</Literal> application---not bad, huh? :-)
57 <Sect1 id="win32-dlls-linking-static">
58 <Title>Not linking with DLLs
59 <IndexTerm><Primary>-static option (Win32)</Primary></IndexTerm></Title>
62 If you want to build an executable that doesn't depend on any
63 ghc-compiled DLLs, use the <Option>-static</Option> option to link in
68 Notice that you cannot mix code that has been compiled with
69 <Option>-static</Option> and not, so you have to use the <Option>-static</Option>
70 option on all the Haskell modules that make up your application.
75 <Sect1 id="win32-dlls-create">
80 <IndexTerm><Primary>Creating a Win32 DLL</Primary></IndexTerm>
81 <IndexTerm><Primary>--mk-dll</Primary></IndexTerm>
82 Sealing up your Haskell library inside a DLL is quite straightforward;
83 compile up the object files that make up the library, and then build
84 the DLL by issuing the following command:
89 ghc --mk-dll -o HSsuper.dll A.o Super.o B.o libmine.a -lgdi32
94 By feeding the ghc compiler driver the option <Option>--mk-dll</Option>, it
95 will build a DLL rather than produce an executable. The DLL will
96 consist of all the object files and archives given on the command
101 To create a `static' DLL, i.e. one that does not depend on the GHC DLLs, compile up your Haskell code using <Option>-static</Option>, and write a <Filename>.def</Filename> file containing the entry points you want to expose (see <XRef LinkEnd="win32-dlls-foreign"> for an example). Then link the DLL adding the <Option>-static</Option> flag, and <Option>-optdll--def=foo.def</Option>, where <Filename>foo.def</Filename> is the name of your <Filename>.def</Filename> file.
105 A couple of things to notice:
113 Since DLLs correspond to packages (see <XRef LinkEnd="packages">) you need
114 to use <Option>-package-name dll-name</Option> when compiling modules that
115 belong to a DLL. If you don't, Haskell code that calls entry points in that
116 DLL will do so incorrectly, and a crash will result.
122 By default, the entry points of all the object files will
123 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:
126 ghc --mk-dll -o .... -optdll--def -optdllMyDef.def
129 See Microsoft documentation for details, but a module definition file
130 simply lists what entry points you want to export. Here's one that's
131 suitable when building a Haskell COM server DLL:
135 DllCanUnloadNow = DllCanUnloadNow@0
136 DllGetClassObject = DllGetClassObject@12
137 DllRegisterServer = DllRegisterServer@0
138 DllUnregisterServer = DllUnregisterServer@0
145 In addition to creating a DLL, the <Option>--mk-dll</Option> option also
146 creates an import library. The import library name is derived from the
147 name of the DLL, as follows:
150 DLL: HScool.dll ==> import lib: libHScool_imp.a
153 The naming scheme may look a bit weird, but it has the purpose of
154 allowing the co-existence of import libraries with ordinary static
155 libraries (e.g., <Filename>libHSfoo.a</Filename> and <Filename>libHSfoo_imp.a</Filename>.
157 Additionally, when the compiler driver is linking in non-static mode, it
158 will rewrite occurrence of <Option>-lHSfoo</Option> on the command line to
159 <Option>-lHSfoo_imp</Option>. By doing this for you, switching from
160 non-static to static linking is simply a question of adding
161 <Option>-static</Option> to your command line.
171 <Sect1 id="win32-dlls-foreign">
172 <Title>Making DLLs to be called from other languages</Title>
176 If you want to package up Haskell code to be called from other languages,
177 such as Visual Basic or C++, there are some extra things it is useful to
178 know. The dirty details are in the <Emphasis>Foreign Function
179 Interface</Emphasis> definition, but it can be tricky to work out how to
180 combine this with DLL building, so here's an example:
188 Use <Literal>foreign export</Literal> declarations to export the Haskell functions you want to call from the outside. For example,
193 adder :: Int -> Int -> IO Int -- gratuitous use of IO
194 adder x y = return (x+y)
196 foreign export stdcall adder :: Int -> Int -> IO Int
206 ghc -c adder.hs -fglasgow-exts
209 This will produce two files, adder.o and adder_stub.o
215 compile up a <Function>DllMain()</Function> that starts up the Haskell RTS---a possible implementation is:
218 #include <windows.h>
220 extern void startupHaskell(int , char** );
222 static char* args[] = { "ghcDll" };
232 if (reason == DLL_PROCESS_ATTACH) {
233 /* By now, the RTS DLL should have been hoisted in, but we need to start it up. */
234 startupHaskell(1, args);
254 ghc --mk-dll -o adder.dll adder.o adder_stub.o dllMain.o
262 Start using <Function>adder</Function> from VBA---here's how I would <Constant>Declare</Constant> it:
265 Private Declare adder Lib "adder.dll" Alias "adder@8"
266 (ByVal x As Long, ByVal y As Long) As Long
269 Since this Haskell DLL depends on a couple of the DLLs that come with GHC,
270 make sure that they are in scope/visible.
281 ;;; Local Variables: ***
283 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***