Using a package couldn't be simpler: if you're using
--make or GHCi, then most of the installed packages will be
automatically available to your program without any further options. The
exceptions to this rule are covered below in Section 5.8.1, “Using Packages
Building your own packages is also quite straightforward: we provide the Cabal infrastructure which automates the process of configuring, building, installing and distributing a package. All you need to do is write a simple configuration file, put a few files in the right places, and you have a package. See the Cabal documentation for details, and also the Cabal libraries (Distribution.Simple, for example).
GHC only knows about packages that are
installed. To see which packages are installed, use
$ ghc-pkg list /usr/lib/ghc-6.4/package.conf: base-1.0, haskell98-1.0, template-haskell-1.0, mtl-1.0, unix-1.0, Cabal-1.0, haskell-src-1.0, parsec-1.0, network-1.0, QuickCheck-1.0, HUnit-1.1, fgl-1.0, X11-1.1, HGL-3.1, OpenGL-2.0, GLUT-2.0, stm-1.0, readline-1.0, (lang-1.0), (concurrent-1.0), (posix-1.0), (util-1.0), (data-1.0), (text-1.0), (net-1.0), (hssource-1.0), rts-1.0
An installed package is either exposed or hidden
by default. Packages hidden by default are listed in
(lang-1.0)) in the output above. Command-line flags, described below, allow you to expose a hidden package
or hide an exposed one.
Only modules from exposed packages may be imported by your Haskell code; if
you try to import a module from a hidden package, GHC will emit an error
To see which modules are provided by a package use the
ghc-pkg command (see Section 5.8.6, “Package management (the
$ ghc-pkg field network exposed-modules exposed-modules: Network.BSD, Network.CGI, Network.Socket, Network.URI, Network
The GHC command line options that control packages are:
This option causes the installed
P to be exposed. The
P can be specified in
full with its version number
network-1.0) or the version
number can be omitted if there is only one version of the
package installed. If there are multiple versions
P installed, then all other
versions will become hidden.
option also causes package
be linked into the resulting executable or shared
object. Whether a packages' library is linked statically
or dynamically is controlled by the flag
––interactive mode (see
Section 5.4, “Modes of operation”), the compiler normally
determines which packages are required by the current
Haskell modules, and links only those. In batch mode
however, the dependency information isn't available, and
-package options must be given when linking. The one other time you might need to use
-package to force linking a package is
when the package does not contain any Haskell modules (it
might contain a C library only, for example). In that
case, GHC will never discover a dependency on it, so it
has to be mentioned explicitly.
For example, to link a program consisting of objects
we made use of the
network package, we need to
give GHC the
-package flag thus:
$ ghc -o myprog Foo.o Main.o -package network
The same flag is necessary even if we compiled the modules from source, because GHC still reckons it's in batch mode:
$ ghc -o myprog Foo.hs Main.hs -package network
Ignore the exposed flag on installed packages, and hide them
all by default. If you use
this flag, then any packages you require (including
base) need to be explicitly exposed using
This is a good way to insulate your program from
differences in the globally exposed packages, and being
explicit about package dependencies is a Good Thing.
Cabal always passes the
-hide-all-packages flag to GHC, for
exactly this reason.
This option does the opposite of
causes the specified package to be hidden,
which means that none of its modules will be available for import
Note that the package might still end up being linked into the final program, if it is a dependency (direct or indirect) of another exposed package.
Causes the compiler to behave as if package
P, and any packages that depend on
P, are not installed at all.
-ignore-package P is the same as
-hide-package flags for
P and all the packages that depend on
P. Sometimes we don't know ahead of time which
packages will be installed that depend on
which is when the
-ignore-package flag can be
Tells GHC the the module being compiled forms part of
If this flag is omitted (a very common case) then the
main is assumed.
Note: the argument to
should be the full package identifier for the package,
that is it should include the version number. For example:
Every complete Haskell program must define
main. (Omitting the
-package-name flag compiles
code for package
main.) Failure to do so leads to a somewhat obscure
link-time error of the form:
/usr/bin/ld: Undefined symbols: _ZCMain_main_closure ___stginit_ZCMain
It is possible that by using packages you might end up with a program that contains two modules with the same name: perhaps you used a package P that has a hidden module M, and there is also a module M in your program. Or perhaps the dependencies of packages that you used contain some overlapping modules. Perhaps the program even contains multiple versions of a certain package, due to dependencies from other packages.
None of these scenarios gives rise to an error on its
own, but they may have some interesting
consequences. For instance, if you have a type
M.T from version 1 of package
P, then this is not the
same as the type
M.T from version 2 of package
P, and GHC will report an error if you try to
use one where the other is expected.
Formally speaking, in Haskell 98, an entity (function, type or class) in a program is uniquely identified by the pair of the module name in which it is defined and its name. In GHC, an entity is uniquely defined by a triple: package, module, and name.
A package database is a file, normally called
package.conf which contains descriptions of installed
packages. GHC usually knows about two package databases:
The global package database, which comes with your GHC installation.
A package database private to each user. On Unix
systems this will be
$HOME/.ghc/, and on
Windows it will be something like
C:\Documents And Settings\.
ghc-pkg tool knows where this file should be
located, and will create it if it doesn't exist (see Section 5.8.6, “Package management (the
When GHC starts up, it reads the contents of these two package
databases, and builds up a list of the packages it knows about. You can
see GHC's package table by running GHC with the
Package databases may overlap: for example, packages in the user database will override those of the same name in the global database.
You can control the loading of package databases using the following GHC options:
Read in the package configuration file
file in addition to the system
default file and the user's local file. Packages in additional
files read this way will override those in the global and user
Prevent loading of the user's local package database.
To create a new package database, just create
a new file and put the string
” in it. Packages can be
added to the file using the
ghc-pkg tool, described in Section 5.8.6, “Package management (the
GHC_PACKAGE_PATH environment variable may be
set to a
on Windows) list of files containing package databases. This list of
package databases is used by GHC and ghc-pkg, with earlier databases in
the list overriding later ones. This order was chosen to match the
behaviour of the
PATH environment variable; think of
it as a list of package databases that are searched left-to-right for
GHC_PACKAGE_PATH ends in a separator, then
the default user and system package databases are appended, in that
order. e.g. to augment the usual set of packages with a database of
your own, you could say (on Unix):
$ export GHC_PACKAGE_PATH=$HOME/.my-ghc-packages.conf:
; instead of
To check whether your
is doing the right thing,
ghc-pkg list will list all
the databases in use, in the reverse order they are searched.
We don't recommend building packages the hard way. Instead, use the Cabal infrastructure if possible. If your package is particularly complicated or requires a lot of configuration, then you might have to fall back to the low-level mechanisms, so a few hints for those brave souls follow.
You need to build an "installed package info" file for
ghc-pkg when installing your
package. The contents of this file are described in
Section 5.8.7, “
InstalledPackageInfo: a package specification
The Haskell code in a package may be built into one or more
archive libraries (e.g.
libHSfoo.a), or a
single shared object
restriction to a single shared object is because the package
system is used to tell the compiler when it should make an
inter-shared-object call rather than an intra-shared-object-call
call (inter-shared-object calls require an extra
Building a static library is done by using the
ar tool, like so:
ar cqs libHSfoo-1.0.a A.o B.o C.o ...
B.o and so on are the compiled Haskell
libHSfoo.a is the library you
wish to create. The syntax may differ slightly on your system,
so check the documentation if you run into difficulties.
Versions of the Haskell libraries for use with GHCi may also
abe included: GHCi cannot load
directly, instead it will look for an object file
HSfoo.o and load that. On some
ghc-pkg tool can automatically
build the GHCi version of each library, see
Section 5.8.6, “Package management (the
ghc-pkg command)”. To build these libraries
by hand from the
.a archive, it is possible
to use GNU ld as follows:
ld -r ––whole-archive -o HSfoo.o libHSfoo.a
–all_load on MacOS X)
When building the package as shared object, GHC wraps
out the underlying linker so that the user gets a common
interface to all shared object variants that are supported
by GHC (DLLs, ELF DSOs, and Mac OS dylibs). The shared
object must be named in specific way for two reasons: (1)
the name must contain the GHC compiler version, so that two
library variants don't collide that are compiled by
different versions of GHC and that therefore are most likely
incompatible with respect to calling conventions, (2) it
must be different from the static name otherwise we would
not be able to control the linker as precisely as necessary
work, see Section 5.10.7, “Options affecting linking”.
ghc -shared libHSfoo-1.0-ghc
GHCVersion.so A.o B.o C.o
Using GHC's version number in the shared object name
allows different library versions compiled by different GHC
versions to be installed in standard system locations,
e.g. under *nix /usr/lib. To obtain the version number of
ghc --numeric-version and use
its output in place
GHCVersion. See also
Section 5.10.6, “Options affecting code generation” on how object files must
be prepared for shared object linking.
GHC does not maintain detailed cross-package dependency information. It does remember which modules in other packages the current module depends on, but not which things within those imported things.
To compile a module which is to be part of a new package,
-package-name option (Section 5.8.1, “Using Packages
Failure to use the
when compiling a package will probably result in disaster, but
you will only discover later when you attempt to import modules
from the package. At this point GHC will complain that the
package name it was expecting the module to come from is not the
same as the package name stored in the
It is worth noting with shared objects, when each package
is built as a single shared object file, since a reference to a shared object costs an extra
indirection, intra-package references are cheaper than
inter-package references. Of course, this applies to the
main package as well.
ghc-pkg tool allows packages to be
added or removed from a package database. By default,
the system-wide package database is modified, but alternatively
the user's local package database or another specified
file can be used.
To see what package databases are in use, say
ghc-pkg list. The stack of databases that
ghc-pkg knows about can be modified using the
GHC_PACKAGE_PATH environment variable (see Section 18.104.22.168, “The
GHC_PACKAGE_PATH environment variable”, and using
--package-conf options on the
ghc-pkg command line.
When asked to modify a database,
the global database by default. Specifying
causes it to act on the user database, or
can be used to act on another database entirely. When multiple of these
options are given, the rightmost one is used as the database to act
Commands that query the package database (list, latest,
describe, field) operate on the list of databases specified by
--package-conf. If none of these flags are
given, the default is
If the environment variable
set, and its value does not end in a separator (
; on Windows), then the last database is
considered to be the global database, and will be modified by default by
ghc-pkg. The intention here is that
GHC_PACKAGE_PATH can be used to create a virtual
package environment into which Cabal packages can be installed without
setting anything other than
ghc-pkg program may be run in the ways listed
below. Where a package name is required, the package can be named in
full including the version number
network-1.0), or without the version number.
Naming a package without the version number matches all versions of the
package; the specified action will be applied to all the matching
packages. A package specifier that matches all version of the package
can also be written
to make it clearer that multiple packages are being matched.
Reads a package specification from
file (which may be “
to indicate standard input),
and adds it to the database of installed packages. The syntax of
file is given in Section 5.8.7, “
InstalledPackageInfo: a package specification
The package specification must be a package that isn't already installed.
The same as
register, except that if a
package of the same name is already installed, it is
replaced by the new one.
Remove the specified package from the database.
exposed flag for package
Check consistency of dependencies in the package database, and report packages that have missing dependencies.
exposed flag for package
ghc-pkg list [
This option displays the currently installed
packages, for each of the databases known to
ghc-pkg. That includes the global database, the
user's local database, and any further files specified using the
-f option on the command line.
Hidden packages (those for which the
False) are shown in parentheses in the
list of packages.
If an optional package identifier
is given, then only packages matching that identifier are
If the option
--simple-output is given, then
the packages are listed on a single line separated by spaces, and
the database names are not included. This is intended to make it
easier to parse the output of
ghc-pkg list using
This option lists registered packages exposing module
$ ghc-pkg find-module Var c:/fptools/validate/ghc/driver/package.conf.inplace: (ghc-6.9.20080428) $ ghc-pkg find-module Data.Sequence c:/fptools/validate/ghc/driver/package.conf.inplace: containers-0.1
Otherwise, it behaves like
Prints the latest available version of package
Emit the full description of the specified package. The
description is in the form of an
InstalledPackageInfo, the same as the input file
ghc-pkg register. See Section 5.8.7, “
InstalledPackageInfo: a package specification
” for details.
If the pattern matches multiple packages, the
description for each package is emitted, separated by the
--- on a line by itself.
Show just a single field of the installed package description
P. Multiple fields can be selected by separating
them with commas
Emit the full description of every package, in the
form of an
Multiple package descriptions are separated by the
--- on a line by itself.
This is almost the same as
ghc-pkg describe '*', except that
is intended for use by tools that parse the results, so
for example where
ghc-pkg describe '*'
will emit an error if it can't find any packages that
match the pattern,
ghc-pkg dump will
simply emit nothing.
Substring matching is supported for
find-module and for
field, where a
'*' indicates open
substring ends (
*infix*). Examples (output omitted):
-- list all regex-related packages ghc-pkg list '*regex*' --ignore-case -- list all string-related packages ghc-pkg list '*string*' --ignore-case -- list OpenGL-related packages ghc-pkg list '*gl*' --ignore-case -- list packages exporting modules in the Data hierarchy ghc-pkg find-module 'Data.*' -- list packages exporting Monad modules ghc-pkg find-module '*Monad*' -- list names and maintainers for all packages ghc-pkg field '*' name,maintainer -- list location of haddock htmls for all packages ghc-pkg field '*' haddock-html -- dump the whole database ghc-pkg describe '*'
Additionally, the following flags are accepted by
Automatically generate the GHCi
.o version of each
.a Haskell library, using GNU ld (if
that is available). Without this option,
ghc-pkg will warn if GHCi versions of
any Haskell libraries in the package don't exist.
.o libraries don't
necessarily have to live in the same directory as the
.a library. However,
this option will cause the GHCi library to be created in
the same directory as the
file to the stack of package
also be the database modified by a
hide command, unless it is overridden by a later
ghc-pkg to ignore missing
dependencies, directories and libraries when registering a package,
and just go ahead and add it anyway. This might be useful if your
package installation system needs to add the package to
GHC before building and installing the files.
Operate on the global package database (this is the default).
This flag affects the
Outputs the command-line syntax.
Operate on the current user's local package database.
This flag affects the
ghc-pkg version number.
When modifying the package database
file, a copy of the original file is
so in an emergency you can always restore the old settings by
copying the old file back again.
A package specification is a Haskell record; in particular, it is the record InstalledPackageInfo in the module Distribution.InstalledPackageInfo, which is part of the Cabal package distributed with GHC.
InstalledPackageInfo has a human
readable/writable syntax. The functions
showInstalledPackageInfo read and write this syntax
respectively. Here's an example of the
InstalledPackageInfo for the
$ ghc-pkg describe unix name: unix version: 1.0 license: BSD3 copyright: maintainer: email@example.com stability: homepage: package-url: description: category: author: exposed: True exposed-modules: System.Posix, System.Posix.DynamicLinker.Module, System.Posix.DynamicLinker.Prim, System.Posix.Directory, System.Posix.DynamicLinker, System.Posix.Env, System.Posix.Error, System.Posix.Files, System.Posix.IO, System.Posix.Process, System.Posix.Resource, System.Posix.Temp, System.Posix.Terminal, System.Posix.Time, System.Posix.Unistd, System.Posix.User, System.Posix.Signals.Exts import-dirs: /usr/lib/ghc-6.4/libraries/unix library-dirs: /usr/lib/ghc-6.4/libraries/unix hs-libraries: HSunix extra-libraries: HSunix_cbits, dl include-dirs: /usr/lib/ghc-6.4/libraries/unix/include includes: HsUnix.h depends: base-1.0
The full Cabal documentation is still in preparation (at time of writing), so in the meantime here is a brief description of the syntax of this file:
A package description consists of a number of field/value pairs. A
field starts with the field name in the left-hand column followed by a
:”, and the value continues until the next line that begins in the
left-hand column, or the end of file.
The syntax of the value depends on the field. The various field types are:
Any arbitrary string, no interpretation or parsing is done.
A sequence of non-space characters, or a sequence of arbitrary
characters surrounded by quotes
A sequence of strings, separated by commas. The sequence may be empty.
In addition, there are some fields with special syntax (e.g. package names, version, dependencies).
The allowed fields, with their types, are:
The package's name (without the version).
The package's version, usually in the form
A.B (any number of components are allowed).
(string) The type of license under which this package is distributed.
This field is a value of the
(optional string) The name of a file giving detailed license information for this package.
(optional freeform) The copyright string.
(optinoal freeform) The email address of the package's maintainer.
(optional freeform) A string describing the stability of the package (eg. stable, provisional or experimental).
(optional freeform) URL of the package's home page.
(optional freeform) URL of a downloadable distribution for this package. The distribution should be a Cabal package.
(optional freeform) Description of the package.
(optinoal freeform) Which category the package belongs to. This field is for use in conjunction with a future centralised package distribution framework, tentatively titled Hackage.
(optional freeform) Author of the package.
(bool) Whether the package is exposed or not.
(string list) modules exposed by this package.
(string list) modules provided by this package, but not exposed to the programmer. These modules cannot be imported, but they are still subject to the overlapping constraint: no other package in the same program may provide a module of the same name.
(string list) A list of directories containing interface files
.hi files) for this package.
If the package contains profiling libraries, then
the interface files for those library modules should have
.p_hi. So the package can
contain both normal and profiling versions of the same
library without conflict (see also
(string list) A list of directories containing libraries for this package.
(string list) A list of libraries containing Haskell code for this
package, with the
.dll suffix omitted. When packages are
built as libraries, the
lib prefix is also omitted.
For use with GHCi, each library should have an
object file too. The name of the object file does
not have a
prefix, and has the normal object suffix for your
For example, if we specify a Haskell library as
HSfoo in the package spec, then the
various flavours of library that GHC actually uses will be
The name of the library on Unix and Windows (mingw) systems. Note that we don't support building dynamic libraries of Haskell code on Unix systems.
The name of the dynamic library on Windows systems (optional).
The object version of the library used by GHCi.
(string list) A list of extra libraries for this package. The
extra-libraries is that
hs-libraries normally have several
versions, to support profiling, parallel and other build
options. The various versions are given different
suffixes to distinguish them, for example the profiling
version of the standard prelude library is named
libHSbase_p.a, with the
_p indicating that this is a profiling
version. The suffix is added automatically by GHC for
hs-libraries only, no suffix is added
for libraries in
The libraries listed in
extra-libraries may be any libraries
supported by your system's linker, including dynamic
.so on Unix,
.DLL on Windows).
extra-libraries are placed
on the linker command line after the
hs-libraries for the same package. If
your package has dependencies in the other direction (i.e.
extra-libraries depends on
hs-libraries), and the libraries are
static, you might need to make two separate
(string list) A list of directories containing C includes for this package.
(string list) A list of files to include for via-C compilations using this package. Typically the include file(s) will contain function prototypes for any C functions used in the package, in case they end up being called as a result of Haskell functions from the package being inlined.
(package name list) Packages on which this package depends. This field contains
packages with explicit versions are required, except that when
submitting a package to
ghc-pkg register, the
versions will be filled in if they are unambiguous.
(string list) Options to pass to Hugs for this package.
(string list) Extra arguments to be added to the gcc command line when this package is being used (only for via-C compilations).
(string list) Extra arguments to be added to the gcc command line (for linking) when this package is being used.
(string list) On Darwin/MacOS X, a list of directories containing
frameworks for this package. This corresponds to the
-framework-path option. It is ignored on all other
(string list) On Darwin/MacOS X, a list of frameworks to link to. This
corresponds to the
-framework option. Take a look
at Apple's developer documentation to find out what frameworks
actually are. This entry is ignored on all other platforms.
(string list) A list of filenames containing Haddock interface
.haddock files) for this package.
(optional string) The directory containing the Haddock-generated HTML for this package.
 it used to in GHC 6.4, but not since 6.6