GHC/As a library

From HaskellWiki
< GHC
Revision as of 15:57, 8 October 2008 by Nominolo (talk | contribs)
Jump to navigation Jump to search

For instructions on the GHC API with GHC 6.8 or older please refer to GHC/As a library (up to 6.8)

Introduction

GHC's functionality can be useful for more things than just compiling Haskell programs. Important use cases are programs that analyse (and perhaps transform) Haskell code. Others include loading Haskell code dynamically in a GHCi-like manner. For this reason, a lot of GHC's features can be accessed by programs which import the ghc package.

The instructions on this page concern the API of GHC 6.10.1 and above. Please not that the GHC API is still in flux and may change quite significantly between major releases while we (the GHC team) provide new features or simplify certain aspects.


Getting Started

To use the GHC API you of course need GHC 6.10.1 or above and import the ghc package.

ghc -package ghc my_program.hs

Most of the common functionality is provided by the GHC module, but occasionally you may have to import other modules. See the GHC's haddock documentation for a list of these modules.

FIXME: link to haddock docs

A Simple Example

import GHC
import DynFlags ( defaultDynFlags )

main = 
    defaultErrorHandler defaultDynFlags $ do
      runGhc (Just top_dir) $ do
        dflags <- getSessionDynFlags
        setSessionDynFlags dflags
        target <- guessTarget "test_main.hs" Nothing
        setTarget [target]
        load LoadAllTargets