C->Haskell, An Interface Generator for Haskell

hackageDB
  :: [Package]
package
MacPorts entry
FreshPorts entry
Debian packages

This page is no longer updated. Please see https://github.com/haskell/c2hs instead.

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. The idea for the tool was developed during the implementation of Gtk+HS, a Haskell binding for the GUI toolkit GTK+. The latter is no longer under active development, but its successor, Gtk2Hs, also uses C->Haskell.

The latest news:

  • [24 April 2011] Duncan Coutts just released version 0.16.3. It supports marshaller arguments and alignof hooks among some other changes.
  • [3 May 2010] Duncan Coutts just released version 0.16.2, which works with GHC 6.12.
  • [28 February 2009] Duncan Coutts just released version 0.16.0 "Crystal Seed", which uses the new package Language.C that Benedikt Huber recently extracted from C->Haskell's code base. Moreover, it has better support for OS X, implements enum hooks, and had several bugs fixed.
  • [24 November 2007] Duncan Coutts just released version 0.15.1 "Rainy Days". This release builds with GHC 6.4 through to 6.8 and also properly supports OpenBSD and Windows. Moreover, the documentation has been updated.
  • [31 August 2007] Duncan Coutts just released version 0.15.0 "Rainy Days". This release includes Duncan's new parser with excellent gcc compatibility and an improved build system.
  • [2 January 2006] Updated the the C->Haskell darcs repository with modifications by Ganesh Sittampalam to fix a problem when pulling on windows/cygwin.
  • [14 May 2006] The C->Haskell darcs repository moved to darcs.haskell.org and Duncan Coutts is now a second maintainer of the tool. (He's the one who heroically rewrote C->Haskell's parser.)
  • [12 December 2005] Source release 0.14.5 brings in addition to bug fixes support for typedef'ed pointers to structs/unions (thanks to Jelmer Vernooij) and enables to suppress code generation for pointer hooks with a nocode directive.

Currently, the C->Haskell compiler is available for the Glasgow Haskell Compiler only. However, the only extension needed over Haskell 98 and the H98 FFI Addendum are existential data types. (The latter are only needed for the C->Haskell compiler itself, but not for the accompanying marshaling libraries.) Partial support for nhc98 is in place, but so far there have always been some bugs in nhc98 that prevented to complete the port.

Download

The code is available from hackage.

Mailing List

To subscribe to the C->Haskell mailing list, visit the subscription page. The address of the list is c2hs@haskell.org and there is also an online archive.

Darcs Repository

For those living on the bleeding edge of development, get the latest C->Haskell code per darcs with

darcs get http://code.haskell.org/c2hs/

Inside your copy of the repository, execute autoconf; chmod +x Setup.hs. Afterwards compilation/installation is as from a tar ball (i.e., Cabal style ./Setup.hs configure; ./Setup.hs build; ./Setup.hs install). Details are in the file INSTALL.

NB for Mac users: One of the patches originating from the cvs to darcs conversion of the repository seems to cause problems on Macs (due to the lack of case distinction in file names). The patch can be circumvented by getting a partial repository as follows:

darcs get --partial http://code.haskell.org/c2hs/

Bug reports and feature requests

We'll use Trac to track bugs and feature requests in the future.

Documentation

If you plan to install C->Haskell, you probably like to read the C->Haskell Tutorial. There is also a research paper about the tool. To get an idea of how to use the interface generator -- after the build -- go to the directory c2hs/tests/, which contains a number of small examples of binding files with matching C header files. Most interesting is Structs.chs as well as Pointer.chs as well as the corresponding C source and header files. The binding file contains instructions on how to generate an executable from it.

Here are two blog posts that provide a more tutorial-style introduction to C->Haskell:

Future Development

If you have any comments, suggestions, or a bug report, please send them to c2hs@haskel.org. Code contributions are of course especially welcome. Just darcs send them.

License

The code is released under the GNU General Public Licence (GPL). This means that you can basically do with it what you want as long as you contribute improvements of the tool itself back to the community. Any interfaces that you generate with the tool are, however, free from any license restrictions from my side. The situation is exactly the same as with the GNU C Compiler (gcc).

• Copyright [2005..2009] Manuel M T Chakravarty • Last modified: 1 March 2009