LinuxCommandLibrary

ghc

Compile Haskell programs

TLDR

Find and compile all modules in the current directory

$ ghc Main
copy

Compile a single file
$ ghc [path/to/file.hs]
copy

Compile using extra optimization
$ ghc -O [path/to/file.hs]
copy

Stop compilation after generating object files (.o)
$ ghc -c [path/to/file.hs]
copy

Start a REPL (interactive shell)
$ ghci
copy

Evaluate a single expression
$ ghc -e [expression]
copy

SYNOPSIS

ghc [-o outfile] [options] file...
ghc --make [options] [target]...

PARAMETERS

-o file
    Specify output filename for executable.

-O[0-2]
    Set optimization level: -O0 none, -O1 moderate, -O2 maximum (default -O0).

--make
    Compile and link in one step, like Make; tracks dependencies automatically.

-Wall
    Enable all common warnings for robust code.

-Werror
    Treat all warnings as errors.

-idir[:dir]
    Add directories to import search path.

-package name
    Expose specified package.

-threaded
    Enable threaded runtime for parallelism.

-dynamic
    Produce dynamically-linked executable.

-hidir dir
    Directory for interface (.hi) files.

-odir dir
    Directory for object (.o) files.

-fforce-recomp
    Disable recompilation checking; force rebuild.

-main-is mod
    Specify main module if not Main.

-v
    Verbose output, show compilation steps.

-h
    Display help summary.

DESCRIPTION

The ghc command is the primary compiler for the Haskell programming language, developed by the Glasgow Haskell Compiler team at the University of Glasgow. It compiles Haskell source files (.hs or .lhs) into highly optimized native machine code executables for various architectures, including x86_64 Linux.

GHC supports a rich type system, lazy evaluation, functional programming paradigms, and advanced features like type classes, GADTs, and parallelism via libraries such as par-monads. It performs whole-program optimization, dead code elimination, and inlining for performance comparable to C.

Common workflow: write Haskell modules, compile with ghc file.hs to produce an executable, or use ghc --make for multi-module projects. It integrates with package managers like Cabal or Stack for dependencies. GHC also includes an interactive REPL via ghci.

Ideal for systems programming, web backends (e.g., Yesod), data processing, and research, GHC powers production systems at Facebook, Standard Chartered, and others.

CAVEATS

GHC requires Haskell sources to be pure; IO via monads. Large projects need Cabal/Stack for deps. Optimizations increase compile time/memory. Not installed by default on minimal Linux; use package manager (e.g., apt install ghc). Version-specific flags may change.

BASIC EXAMPLE

echo 'main = putStrLn "Hello, Haskell!"' > hello.hs
ghc hello.hs -o hello
./hello

Produces hello executable.

INSTALLATION

On Debian/Ubuntu: sudo apt install ghc. Use Haskell Platform or Stack for full env. Check version: ghc --version.

HISTORY

Developed since 1989 at University of Glasgow as Lazy ML Compiler; first GHC release 1991. Haskell 98 standard in 1998 boosted adoption. GHC 6.0 (2005) introduced Template Haskell; 7.0 (2010) better parallelism. Now at 9.8+ (2024), with linear types, improvements in performance/STG machine. Maintained by Well-Typed, Tweag, and community.

SEE ALSO

ghci(1), runghc(1), cabal(1), stack(1), haskell-mode(??)

Copied to clipboard