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, Dynamic, and Mode 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 Overriding the default behaviour for a file
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. The main package
4.8.3. Consequences of packages for the Haskell language
4.8.4. Package Databases The GHC_PACKAGE_PATH environment variable
4.8.5. Package IDs, dependencies, and broken packages
4.8.6. Package management (the ghc-pkg command)
4.8.7. Building a package from Haskell source
4.8.8. 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 CPP and string gaps
4.10.4. Options affecting a Haskell pre-processor
4.10.5. Options affecting code generation
4.10.6. Options affecting linking
4.11. Using shared libraries
4.11.1. Building programs that use shared libraries
4.11.2. Shared libraries for Haskell packages
4.11.3. Shared libraries that export a C API
4.11.4. Finding shared libraries at runtime
4.12. Using Concurrent Haskell
4.13. Using SMP parallelism
4.13.1. Compile-time options for SMP parallelism
4.13.2. RTS options for SMP parallelism
4.13.3. Hints for using SMP parallelism
4.14. Platform-specific Flags
4.15. Running a compiled program
4.15.1. Setting global RTS options
4.15.2. Miscellaneous RTS options
4.15.3. RTS options to control the garbage collector
4.15.4. RTS options for concurrency and parallelism
4.15.5. RTS options for profiling
4.15.6. Tracing
4.15.7. RTS options for hackers, debuggers, and over-interested souls
4.15.8. “Hooks” to change RTS behaviour
4.15.9. Getting information about the RTS
4.16. Generating and compiling External Core Files
4.17. Debugging the compiler
4.17.1. Dumping out compiler intermediate structures
4.17.2. Checking for consistency
4.17.3. How to read Core syntax (from some -ddump flags)
4.17.4. Unregisterised compilation
4.18. Flag reference
4.18.1. Help and verbosity options
4.18.2. Which phases to run
4.18.3. Alternative modes of operation
4.18.4. Redirecting output
4.18.5. Keeping intermediate files
4.18.6. Temporary files
4.18.7. Finding imports
4.18.8. Interface file options
4.18.9. Recompilation checking
4.18.10. Interactive-mode options
4.18.11. Packages
4.18.12. Language options
4.18.13. Warnings
4.18.14. Optimisation levels
4.18.15. Individual optimisations
4.18.16. Profiling options
4.18.17. Program coverage options
4.18.18. Haskell pre-processor options
4.18.19. C pre-processor options
4.18.20. Code generation options
4.18.21. Linking options
4.18.22. Replacing phases
4.18.23. Forcing options to particular phases
4.18.24. Platform-specific options
4.18.25. External core file options
4.18.26. Compiler debugging options
4.18.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 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.13, “Pragmas”).

Only dynamic flags can be used in an OPTIONS_GHC pragma (see Section 4.2, “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.1.3. Setting options in GHCi

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