4.2. Options overview

GHC's behaviour is controlled by options, which for historical reasons are also sometimes referred to as command-line flags or arguments. Options can be specified in three ways:

4.2.1. Command-line arguments

An invocation of GHC takes the following form:

ghc [argument...]

Command-line arguments are either options or file names.

Command-line options begin with -. They may not be grouped: -vO is different from -v -O. Options need not precede filenames: e.g., ghc *.o -o foo. All options are processed and then applied to all files; you cannot, for example, invoke ghc -c -O1 Foo.hs -O2 Bar.hs to apply different optimisation levels to the files Foo.hs and Bar.hs.

4.2.2. Command line options in source files

Sometimes it is useful to make the connection between a source file and the command-line options it requires quite tight. For instance, if a Haskell source file deliberately uses name shadowing, it should be compiled with the -fno-warn-name-shadowing option. Rather than maintaining the list of per-file options in a Makefile, it is possible to do this directly in the source file using the OPTIONS_GHC pragma :

{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module X where
...

OPTIONS_GHC is a file-header pragma (see Section 7.18, “Pragmas”).

Only dynamic flags can be used in an OPTIONS_GHC pragma (see Section 4.3, “Static, Dynamic, and Mode options”).

Note that your command shell does not get to the source file options, they are just included literally in the array of command-line arguments the compiler maintains internally, so you'll be desperately disappointed if you try to glob etc. inside OPTIONS_GHC.

NOTE: the contents of OPTIONS_GHC are appended to the command-line options, so options given in the source file override those given on the command-line.

It is not recommended to move all the contents of your Makefiles into your source files, but in some circumstances, the OPTIONS_GHC pragma is the Right Thing. (If you use -keep-hc-file and have OPTION flags in your module, the OPTIONS_GHC will get put into the generated .hc file).

4.2.3. Setting options in GHCi

Options may also be modified from within GHCi, using the :set command. See Section 2.8, “The :set and :seti commands” for more details.