ghc
Compile Haskell programs
TLDR
Find and compile all modules in the current directory
Compile a single file
Compile using extra optimization
Stop compilation after generating object files (.o)
Start a REPL (interactive shell)
Evaluate a single expression
SYNOPSIS
ghc [OPTIONS] FILES...
ghc --make [OPTIONS] [MODULES...]
ghc -e EXPRESSION
ghc --help
ghc --version
PARAMETERS
-o OUTPUT
Specify the output file name for the compiled executable or library.
--make
Compile multiple modules and link them into a single executable or library, resolving dependencies automatically.
-O[LEVEL]
Enable optimization. LEVEL can be omitted (defaults to 1), or specified as 0 (no optimizations), 1, or 2 (maximum optimizations).
-Wall
Enable all standard warnings, encouraging cleaner and more robust code.
-Werror
Treat all warnings as errors, causing compilation to fail if any warnings are present.
-iDIR[:DIR...]
Add directories to the search path for finding Haskell modules and interface files.
-e EXPRESSION
Evaluate a Haskell expression directly on the command line and print the result, similar to GHCi's interactive evaluation.
--version
Display the compiler's version information and copyright details.
--help
Show a summary of command-line options.
-v[LEVEL]
Set the verbosity level of the compiler output. Higher levels provide more detailed information about the compilation process.
-main-is MODULE.FUNCTION
Specify the main entry point for the program when linking, useful for projects with multiple possible entry points.
DESCRIPTION
GHC, the Glasgow Haskell Compiler, is the most widely used and feature-rich compiler for the Haskell programming language. It can compile Haskell source code into native machine code executables, shared libraries, or static libraries. Beyond compilation, GHC also provides GHCi, an interactive interpreter for Haskell, which is invaluable for rapid prototyping and debugging. It supports the latest Haskell language standards (like Haskell 2010 and GHC2021) and various extensions.
GHC is known for its advanced optimization capabilities, generating highly efficient code, and its strong type system which catches many errors at compile time. It plays a central role in the Haskell ecosystem, being the foundation for most Haskell development tools and libraries.
CAVEATS
GHC can be memory-intensive during compilation, especially for large projects or when aggressive optimizations are enabled.
The build process for complex Haskell projects often relies on external tools like Cabal or Stack to manage dependencies and orchestrate builds, rather than `ghc` directly, which can add a layer of complexity for newcomers.
While highly optimized, the generated binaries might have a larger footprint compared to C programs due to the Haskell runtime system and linked libraries.
INTERACTIVE MODE (GHCI)
GHC includes an interactive interpreter, GHCi, accessible by running `ghc` without arguments or specifically `ghci`. This mode allows users to load modules, evaluate expressions, test functions, and debug code incrementally, making it an indispensable tool for rapid development and learning.
LANGUAGE EXTENSIONS
GHC supports a vast array of language extensions beyond the standard Haskell 2010. These extensions, often enabled via pragmas in source files or command-line flags (e.g., `-XExtensionName`), allow developers to use cutting-edge features, optimize code, or experiment with new programming paradigms.
COMPILATION TARGETS
Beyond native executables, GHC can also compile Haskell code to other targets, including shared libraries (`-shared`), static libraries (`-staticlib`), and bytecode (for GHCi). It also supports cross-compilation, allowing developers to build binaries for different architectures or operating systems.
HISTORY
The Glasgow Haskell Compiler (GHC) originated in the early 1990s at the University of Glasgow. It quickly became the reference implementation for the Haskell 98 standard and has since evolved into the dominant compiler for the Haskell programming language. Its development has been driven by a global community, continuously incorporating new language features, performance optimizations, and platform support. GHC's steady evolution has been crucial in establishing Haskell as a viable language for industrial-strength applications and research, maintaining its position at the forefront of functional programming language implementation.