GHC's behaviour is firstly controlled by a mode flag. Only one of these flags may be given, but it does not necessarily need to be the first option on the command-line.
If no mode flag is present, then GHC will enter make mode
(Section 4.5.1, “Using ghc
––make”) if there are any Haskell source
files given on the command line, or else it will link the
objects named on the command line to produce an executable.
The available mode flags are:
Interactive mode, which is also available as ghci. Interactive mode is described in more detail in Chapter 2, Using GHCi.
In this mode, GHC will build a multi-module Haskell
program automatically, figuring out dependencies for itself.
If you have a straightforward Haskell program, this is
likely to be much easier, and faster, than using
make. Make mode is described in Section 4.5.1, “Using ghc
This mode is the default if there are any Haskell
source files mentioned on the command line, and in this case
––make option can be omitted.
Expression-evaluation mode. This is very similar to
interactive mode, except that there is a single expression
to evaluate (
expr) which is given
on the command line. See Section 4.5.2, “Expression evaluation mode” for
This is the traditional batch-compiler mode, in which GHC can compile source files one at a time, or link objects together into an executable. This mode also applies if there is no other mode flag specified on the command line, in which case it means that the specified files should be compiled and then linked to form a program. See Section 4.5.3, “Batch compiler mode”.
Dependency-generation mode. In this mode, GHC can be
used to generate dependency information suitable for use in
Makefile. See Section 4.7.11, “Dependency generation”.
DLL-creation mode (Windows only). See Section 13.6.1, “Creating a DLL”.
Cause GHC to spew a long usage message to standard output and then exit.
Read the interface in
file and dump it as text to
stdout. For example
ghc --show-iface M.hi.
Print the supported language extensions.
Print information about the compiler.
Print a one-line string including GHC's version number.
Print GHC's numeric version number only.
Print the path to GHC's library directory. This is
the top of the directory tree containing GHC's libraries,
interfaces, and include files (usually something like
/usr/local/lib/ghc-5.04 on Unix). This
is the value of
in the package configuration file
(see Section 4.9, “
In this mode, GHC will build a multi-module Haskell program by following
dependencies from one or more root modules (usually just
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
In fact, GHC enters make mode automatically if there are any Haskell source files on the command line and no other mode is specified, so in this case we could just type
Any number of source file names or module names may be
specified; 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
––make over traditional
GHC doesn't have to be restarted for each compilation,
which means it can cache information between compilations.
Compiling a multi-module program with
––make can be up to twice as fast as
ghc individually on each source
You don't have to write a
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
pragma (see Section 4.2.2, “Command line options in source files”).
If the program needs to be linked with additional objects (say, some auxiliary 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.7.3, “The search path”).
This mode is very similar to interactive mode, except that
there is a single expression to evaluate which is specified on
the command line as an argument to the
Haskell source files may be named on the command line, and they will be loaded exactly as in interactive mode. The expression is evaluated in the context of the loaded modules.
For example, to load and run a Haskell program containing
Main, we might say
ghc -e Main.main Main.hs
or we can just use this mode to evaluate expressions in
the context of the
$ ghc -e "interact (unlines.map reverse.lines)" hello olleh
In batch mode, GHC will compile one or more source files given on the command line.
The first phase to run is determined by each input-file suffix, and the last phase is determined by a flag. If no relevant flag is present, then go all the way through to linking. This table summarises:
|Phase of the compilation system||Suffix saying “start here”||Flag saying “stop after”||(suffix of) output file|
|C pre-processor (opt.)|
|C compiler (opt.)|
Thus, a common invocation would be:
ghc -c Foo.hs
to compile the Haskell source file
Foo.hs to an object file
Note: What the Haskell compiler proper produces depends on what backend code generator is used. See Section 4.11, “GHC Backends” for more details.
Note: C pre-processing is optional, the
flag turns it on. See Section 4.12.3, “Options affecting the C pre-processor” for more
As described above, the way in which a file is processed by GHC
depends on its suffix. This behaviour can be overridden using the