NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.64 ">

4.5. Using ghc ––make

When given the ––make option, GHC will build a multi-module Haskell program by following dependencies from a single root module (usually Main). For example, if your Main module is in a file called Main.hs, you could compile and link the program like this:

ghc ––make Main.hs

The command line must contain one source file or module name; GHC will figure out all the modules in the program by following the imports from this initial module. It will then attempt to compile each module which is out of date, and finally if the top module is Main, the program will also be linked into an executable.

The main advantages to using ghc ––make over traditional Makefiles are:

  • GHC doesn't have to be restarted for each compilation, which means it can cache information between compilations. Compiling a muli-module program with ghc ––make can be up to twice as fast as running ghc individually on each source file.

  • You don't have to write a Makefile.

  • GHC re-calculates the dependencies each time it is invoked, so the dependencies never get out of sync with the source.

Any of the command-line options described in the rest of this chapter can be used with ––make, but note that any options you give on the command line will apply to all the source files compiled, so if you want any options to apply to a single source file only, you'll need to use an OPTIONS pragma (see Section 4.1.2).

If the program needs to be linked with additional objects (say, some auxilliary C code), these can be specified on the command line as usual.

Note that GHC can only follow dependencies if it has the source file available, so if your program includes a module for which there is no source file, even if you have an object and an interface file for the module, then GHC will complain. The exception to this rule is for package modules, which may or may not have source files.

The source files for the program don't all need to be in the same directory; the -i option can be used to add directories to the search path (see Section 4.9.2).