Combining ghc and Tcl/Tk on Win32 platforms

# Building ghc applications using Tcl/Tk under Win32

Building ghc-compiled applications under Win32 that calls upon Tcl/Tk will not work out of the box. This note outlines what you need to do in order to make it work.

## The problem

Starting with 4.03, ghc dropped the dependency on the Cygwin DLL for the binaries that it produces. An upshot of this change is that working with code that uses the Cygwin DLL is not going to work (most of the time.) One main reason for the incompatibility is that you're combining code (in object or DLL form) that uses a separate C run-time libraries, which causes Bad Things to happen.

The cygwin B20.1 distribution comes with Tcl/Tk 8.0 included, DLLs which (naturally) have been compiled up to depend on the cygwin DLL. For reasons 'outlined' in the previous paragraph, using these Tcl/Tk DLLs from ghc-compiled code is not going to work very well.

## The solution

To avoid depending on the Tcl/Tk that comes with the Cygwin dist, here's what you need to do:

- Download and install a 'pure' Win32 version of Tcl/Tk from Scriptics.

  In the case of TclHaskell, I don't know what the version requirements are, but it should at least work together with Tcl/Tk 8.0; perhaps newer releases as well..?

- Get hold of the 'pexports' util. You may already have a copy of Matt Pietrek's handy PE-dumping util lying about; if not, a (souped up) version of same is available from

      http://www.acc.umu.se/~anorland/gnu-win32/index.html

  Once you have downloaded `pexports-0.41.zip`, you'll want to be using `mingwbin/pexports.exe` from within that bundle. Install it somewhere along your PATH.

- Pick up a (GNU) Makefile which contains some rules for generating a GNU linker-friendly import library (.a) from a Win32 DLL. It is available from here

  Put a copy of this `Makefile` in the `bin/` directory that the Scriptics installer for Tcl/Tk left behind. Open up a command shell window of choice and `cd` down into that directory and type `make` (where `make` == GNU make.) All being well (`make`, `pexports` and `dlltool`) being reachable via your PATH), it should then produce a collection of `.a` import libs corresponding to the DLLs in that directory.

- You're now all set to start compiling and linking. When linking, you need to extend the command line with `-L/path/to/TclDir` (Note: if that path is `c:\Program Files\Tcl\bin`, you may find it easier finding it easier working with the mangled MSDOS-style directory names, i.e., use `-L/Progra~1/Tcl/bin` instead.)

- Note: When compiling up .c files that you want to link with ghc-compiled code, remember to always supply the option `-mno-cygwin` to gcc.

  The ghc-win32 driver takes care of feeding this option to gcc whenever it invokes it, so it may be worth your while modifying your Makefiles, substituting `gcc` with `ghc` ("GHC, a better GCC" ;-)

Notice that there's nothing inherently Tcl/Tk specific with the above sequence of tasks. Indeed, the procedure is a general recipe for generating ghc (and 'gcc -mno-cygwin)-friendly import libraries from any old Win32 DLLs.