Compiling programs with ghc

Running ghc invokes the Glasgow Haskell Compiler (GHC), and can be used to compile Haskell modules and programs into native executables and libraries.

Create a new Haskell source file named hello.hs, and write the following code in it:

main = putStrLn "Hello, Haskell!"

Now, we can compile the program by invoking ghc with the file name:

➜ ghc hello.hs
[1 of 1] Compiling Main             ( hello.hs, hello.o )
Linking hello ...

GHC created the following files:

GHC will produce an executable when the source file satisfies both conditions:

  1. Defines the main function in the source file

  2. Defines the module name to be Main or does not have a module declaration

Otherwise, it will only produce the .o and .hi files.