Chapter 4. Using GHC

Table of Contents

4.1. Options overview
4.1.1. command-line arguments
4.1.2. command line options in source files
4.1.3. Setting options in GHCi
4.2. Static vs. Dynamic options
4.3. Meaningful file suffixes
4.4. Modes of operation
4.4.1. Using ghc ––make
4.4.2. Expression evaluation mode
4.4.3. Batch compiler mode
4.5. Help and verbosity options
4.6. Filenames and separate compilation
4.6.1. Haskell source files
4.6.2. Output files
4.6.3. The search path
4.6.4. Redirecting the compilation output(s)
4.6.5. Keeping Intermediate Files
4.6.6. Redirecting temporary files
4.6.7. Other options related to interface files
4.6.8. The recompilation checker
4.6.9. How to compile mutually recursive modules
4.6.10. Using make
4.6.11. Dependency generation
4.6.12. Orphan modules and instance declarations
4.7. Warnings and sanity-checking
4.8. Packages
4.8.1. Using Packages
4.8.2. Package Databases
4.8.3. Building a package from Haskell source
4.8.4. Package management (the ghc-pkg command)
4.8.5. InstalledPackageInfo: a package specification
4.9. Optimisation (code improvement)
4.9.1. -O*: convenient “packages” of optimisation flags.
4.9.2. -f*: platform-independent flags
4.10. Options related to a particular phase
4.10.1. Replacing the program for one or more phases
4.10.2. Forcing options to a particular phase
4.10.3. Options affecting the C pre-processor
4.10.3.1. CPP and string gaps
4.10.4. Options affecting a Haskell pre-processor
4.10.5. Options affecting the C compiler (if applicable)
4.10.6. Options affecting code generation
4.10.7. Options affecting linking
4.11. Using Concurrent Haskell
4.12. Using parallel Haskell
4.12.1. Dummy's guide to using PVM
4.12.2. parallelism profiles
4.12.3. Other useful info about running parallel programs
4.12.4. RTS options for Concurrent/parallel Haskell
4.13. Platform-specific Flags
4.14. Running a compiled program
4.14.1. Setting global RTS options
4.14.2. RTS options to control the garbage collector
4.14.3. RTS options for profiling and Concurrent/Parallel Haskell
4.14.4. RTS options for hackers, debuggers, and over-interested souls
4.14.5. “Hooks” to change RTS behaviour
4.15. Generating and compiling External Core Files
4.16. Debugging the compiler
4.16.1. Dumping out compiler intermediate structures
4.16.2. Checking for consistency
4.16.3. How to read Core syntax (from some -ddump flags)
4.16.4. Unregisterised compilation
4.17. Flag reference
4.17.1. Help and verbosity options
4.17.2. Which phases to run
4.17.3. Alternative modes of operation
4.17.4. Redirecting output
4.17.5. Keeping intermediate files
4.17.6. Temporary files
4.17.7. Finding imports
4.17.8. Interface file options
4.17.9. Recompilation checking
4.17.10. Interactive-mode options
4.17.11. Packages
4.17.12. Language options
4.17.13. Warnings
4.17.14. Optimisation levels
4.17.15. Individual optimisations
4.17.16. Profiling options
4.17.17. Parallelism options
4.17.18. C pre-processor options
4.17.19. C compiler options
4.17.20. Code generation options
4.17.21. Linking options
4.17.22. Replacing phases
4.17.23. Forcing options to particular phases
4.17.24. Platform-specific options
4.17.25. External core file options
4.17.26. Compiler debugging options
4.17.27. Misc compiler options

4.1. 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.1.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.1.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 uses GHC extensions, it will always need to be compiled with the -fglasgow-exts 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 -fglasgow-exts #-}
module X where
...

OPTIONS_GHC pragmas are only looked for at the top of your source files, upto the first (non-literate,non-empty) line not containing OPTIONS_GHC. Multiple OPTIONS_GHC pragmas are recognised. Do not put comments before, or on the same line as, the OPTIONS_GHC pragma.

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 prepended to the command-line options, so you do have the ability to override OPTIONS_GHC settings via 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-too and have OPTION flags in your module, the OPTIONS_GHC will get put into the generated .hc file).

4.1.3. Setting options in GHCi

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