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:

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.