Chapter 4. Using GHC

Table of Contents

4.1. Getting started: compiling programs
4.2. Options overview
4.2.1. Command-line arguments
4.2.2. Command line options in source files
4.2.3. Setting options in GHCi
4.3. Static, Dynamic, and Mode options
4.4. Meaningful file suffixes
4.5. Modes of operation
4.5.1. Using ghc ––make
4.5.2. Expression evaluation mode
4.5.3. Batch compiler mode
4.5.3.1. Overriding the default behaviour for a file
4.6. Help and verbosity options
4.7. Filenames and separate compilation
4.7.1. Haskell source files
4.7.2. Output files
4.7.3. The search path
4.7.4. Redirecting the compilation output(s)
4.7.5. Keeping Intermediate Files
4.7.6. Redirecting temporary files
4.7.7. Other options related to interface files
4.7.8. The recompilation checker
4.7.9. How to compile mutually recursive modules
4.7.10. Using make
4.7.11. Dependency generation
4.7.12. Orphan modules and instance declarations
4.8. Warnings and sanity-checking
4.9. Packages
4.9.1. Using Packages
4.9.2. The main package
4.9.3. Consequences of packages for the Haskell language
4.9.4. Package Databases
4.9.4.1. The GHC_PACKAGE_PATH environment variable
4.9.5. Package IDs, dependencies, and broken packages
4.9.6. Package management (the ghc-pkg command)
4.9.7. Building a package from Haskell source
4.9.8. InstalledPackageInfo: a package specification
4.10. Optimisation (code improvement)
4.10.1. -O*: convenient “packages” of optimisation flags.
4.10.2. -f*: platform-independent flags
4.11. Options related to a particular phase
4.11.1. Replacing the program for one or more phases
4.11.2. Forcing options to a particular phase
4.11.3. Options affecting the C pre-processor
4.11.3.1. CPP and string gaps
4.11.4. Options affecting a Haskell pre-processor
4.11.5. Options affecting code generation
4.11.6. Options affecting linking
4.12. Using shared libraries
4.12.1. Building programs that use shared libraries
4.12.2. Shared libraries for Haskell packages
4.12.3. Shared libraries that export a C API
4.12.4. Finding shared libraries at runtime
4.13. Using Concurrent Haskell
4.14. Using SMP parallelism
4.14.1. Compile-time options for SMP parallelism
4.14.2. RTS options for SMP parallelism
4.14.3. Hints for using SMP parallelism
4.15. Platform-specific Flags
4.16. Running a compiled program
4.16.1. Setting RTS options
4.16.1.1. Setting RTS options on the command line
4.16.1.2. Setting RTS options at compile time
4.16.1.3. Setting RTS options with the GHCRTS environment variable
4.16.1.4. “Hooks” to change RTS behaviour
4.16.2. Miscellaneous RTS options
4.16.3. RTS options to control the garbage collector
4.16.4. RTS options for concurrency and parallelism
4.16.5. RTS options for profiling
4.16.6. Tracing
4.16.7. RTS options for hackers, debuggers, and over-interested souls
4.16.8. Getting information about the RTS
4.17. Generating and compiling External Core Files
4.18. Debugging the compiler
4.18.1. Dumping out compiler intermediate structures
4.18.2. Checking for consistency
4.18.3. How to read Core syntax (from some -ddump flags)
4.18.4. Unregisterised compilation
4.19. Flag reference
4.19.1. Help and verbosity options
4.19.2. Which phases to run
4.19.3. Alternative modes of operation
4.19.4. Redirecting output
4.19.5. Keeping intermediate files
4.19.6. Temporary files
4.19.7. Finding imports
4.19.8. Interface file options
4.19.9. Recompilation checking
4.19.10. Interactive-mode options
4.19.11. Packages
4.19.12. Language options
4.19.13. Warnings
4.19.14. Optimisation levels
4.19.15. Individual optimisations
4.19.16. Profiling options
4.19.17. Program coverage options
4.19.18. Haskell pre-processor options
4.19.19. C pre-processor options
4.19.20. Code generation options
4.19.21. Linking options
4.19.22. Replacing phases
4.19.23. Forcing options to particular phases
4.19.24. Platform-specific options
4.19.25. External core file options
4.19.26. Compiler debugging options
4.19.27. Misc compiler options

4.1. Getting started: compiling programs

In this chapter you'll find a complete reference to the GHC command-line syntax, including all 400+ flags. It's a large and complex system, and there are lots of details, so it can be quite hard to figure out how to get started. With that in mind, this introductory section provides a quick introduction to the basic usage of GHC for compiling a Haskell program, before the following sections dive into the full syntax.

Let's create a Hello World program, and compile and run it. First, create a file hello.hs containing the Haskell code:

main = putStrLn "Hello, World!"

To compile the program, use GHC like this:

$ ghc hello.hs

(where $ represents the prompt: don't type it). GHC will compile the source file hello.hs, producing an object file hello.o and an interface file hello.hi, and then it will link the object file to the libraries that come with GHC to produce an executable called hello on Unix/Linux/Mac, or hello.exe on Windows.

By default GHC will be very quiet about what it is doing, only printing error messages. If you want to see in more detail what's going on behind the scenes, add -v to the command line.

Then we can run the program like this:

$ ./hello
Hello World!

If your program contains multiple modules, then you only need to tell GHC the name of the source file containing the Main module, and GHC will examine the import declarations to find the other modules that make up the program and find their source files. This means that, with the exception of the Main module, every source file should be named after the module name that it contains (with dots replaced by directory separators). For example, the module Data.Person would be in the file Data/Person.hs on Unix/Linux/Mac, or Data\Person.hs on Windows.