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 may contain any number of source file names or module names; GHC will figure out all the modules in the program by following the imports from these initial modules. It will then attempt to compile each module which is out of date, and finally if there is a Main module, 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), then the object files can be given on the command line and GHC will include them when linking the executable.

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).