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. GHC Backends
4.11.1. Native code Generator (-fasm)
4.11.2. LLVM Code Generator (-fllvm)
4.11.3. C Code Generator (-fvia-C)
4.11.4. Unregisterised compilation
4.12. Options related to a particular phase
4.12.1. Replacing the program for one or more phases
4.12.2. Forcing options to a particular phase
4.12.3. Options affecting the C pre-processor
4.12.3.1. CPP and string gaps
4.12.4. Options affecting a Haskell pre-processor
4.12.5. Options affecting code generation
4.12.6. Options affecting linking
4.13. Using shared libraries
4.13.1. Building programs that use shared libraries
4.13.2. Shared libraries for Haskell packages
4.13.3. Shared libraries that export a C API
4.13.4. Finding shared libraries at runtime
4.13.4.1. Unix
4.13.4.2. Mac OS X
4.14. Using Concurrent Haskell
4.15. Using SMP parallelism
4.15.1. Compile-time options for SMP parallelism
4.15.2. RTS options for SMP parallelism
4.15.3. Hints for using SMP parallelism
4.16. Platform-specific Flags
4.17. Running a compiled program
4.17.1. Setting RTS options
4.17.1.1. Setting RTS options on the command line
4.17.1.2. Setting RTS options at compile time
4.17.1.3. Setting RTS options with the GHCRTS environment variable
4.17.1.4. “Hooks” to change RTS behaviour
4.17.2. Miscellaneous RTS options
4.17.3. RTS options to control the garbage collector
4.17.4. RTS options for concurrency and parallelism
4.17.5. RTS options for profiling
4.17.6. Tracing
4.17.7. RTS options for hackers, debuggers, and over-interested souls
4.17.8. Getting information about the RTS
4.18. Generating and compiling External Core Files
4.19. Debugging the compiler
4.19.1. Dumping out compiler intermediate structures
4.19.2. Formatting dumps
4.19.3. Suppressing unwanted information
4.19.4. Checking for consistency
4.19.5. How to read Core syntax (from some -ddump flags)
4.20. Flag reference
4.20.1. Help and verbosity options
4.20.2. Which phases to run
4.20.3. Alternative modes of operation
4.20.4. Redirecting output
4.20.5. Keeping intermediate files
4.20.6. Temporary files
4.20.7. Finding imports
4.20.8. Interface file options
4.20.9. Recompilation checking
4.20.10. Interactive-mode options
4.20.11. Packages
4.20.12. Language options
4.20.13. Warnings
4.20.14. Optimisation levels
4.20.15. Individual optimisations
4.20.16. Profiling options
4.20.17. Program coverage options
4.20.18. Haskell pre-processor options
4.20.19. C pre-processor options
4.20.20. Code generation options
4.20.21. Linking options
4.20.22. Plugin options
4.20.23. Replacing phases
4.20.24. Forcing options to particular phases
4.20.25. Platform-specific options
4.20.26. External core file options
4.20.27. Compiler debugging options
4.20.28. 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.