The Binding Generator C➔Haskell

Manuel Chakravarty

November 2007

Table of Contents

Where is the Source?
What Else Do I Need?
I Got Everything, and Now?
Usage of C➔Haskell
Usage of c2hs
Compilation of a Generated Haskell API
Implementation of Haskell Binding Modules
Import Hooks
Context Hooks
Type Hooks
Sizeof Hooks
Enumeration Hooks
Call Hooks
Function Hooks
Get Hooks
Set Hooks
Pointer Hooks
Class Hooks
CPP Directives and Inline C Code
Grammar Rules
Bug Reports and Suggestions
C➔Haskell License
Documentation License
Possible Licenses of Generated Code
GNU Free Documentation License
Release Notes
Version 0.15.1 "Rainy Days"
Version 0.14.5 "Travelling Lightly"
Version 0.13.6 "Pressing Forward"
Version 0.12.1 "Springtime"
Version 0.11.5 "Powder Snow"
Version 0.10.17 "Altocumulus Stratiformis Perlucidus Undulatus"
Version 0.9.9 "Blue Ginger"
Version 0.8.2 "Gentle Moon"
Version 0.8.1 "Gentle Moon"
Version 0.7.10 "Afterthought"
Version 0.7.9 "Afterthought"
Version 0.7.8
Version 0.7.7
Version 0.7.6
Version 0.7.5


C➔Haskell is an interface generator that simplifies the development of Haskell bindings to C libraries. The tool processes existing C header files that determine data layout and function signatures on the C side in conjunction with Haskell modules that specify Haskell-side type signatures and marshaling details. Hooks embedded in the Haskell code signal access to C structures and functions -- they are expanded by the interfacing tool in dependence on information from the corresponding C header file. Another noteworthy property is the lightweight nature of the approach.

More background information is available in a research paper discussing C➔Haskell, which is at However, this paper does not cover the more recent advanced features such as function hooks and conditional compilation.


It follows a brief discussion of the installation from source. There is, however, a file INSTALL in the source distribution, which is more frequently updated and should be consulted in any case.

Where is the Source?

The master site of C➔Haskell is at It has all the latest information and sources. Furthermore, it explains how to get access to the C➔Haskell Darcs repository and has references to pre-compiled binaries.

What Else Do I Need?

You need a Haskell system supported by C➔Haskell. Currently, this is only the Glasgow Haskell Compiler (GHC), which you can obtain from Furthermore, you need the Haskell package system Cabal. See the INSTALL file for details on supported versions.

To build the documentation, you will also need the SGML Tools, which you find at your nearest sunsite or Linux mirror or at On an up-to-date Linux system, the tools are probably already installed.

I Got Everything, and Now?

The short answer is

$ tar -xzf package.tar.gz      # unpack the sources
$ cd package                   # change to the toplevel directory
$ runghc Setup.hs configure    # configure the build system
$ runghc Setup.hs build        # build everything
[ Become root if necessary ]
$ runghc Setup.hs install      # install c2hs

In the INSTALL file, there are more details.

Optionally, you can build the documentation by issuing make doc and install it with make install-doc.