c2hs

Generate Haskell FFI bindings from C headers

TLDR

Generate Haskell bindings from C header
$ c2hs [header.h] [bindings.chs]
Specify include path
$ c2hs -C -I[/path/to/includes] [bindings.chs]
Pass preprocessor defines
$ c2hs -C -D[MACRO=value] [bindings.chs]
Specify output directory
$ c2hs -o [output_dir] [bindings.chs]
Show help
$ c2hs --help

SYNOPSIS

c2hs [options] header.h binding.chs

DESCRIPTION

c2hs is a C to Haskell interface generator that simplifies creating FFI bindings to C libraries. It extracts interface information from C header files and generates Haskell code with foreign imports and correct type marshaling.The tool processes .chs files containing Haskell code with embedded hooks that reference C structures and functions.

PARAMETERS

-C options

Pass options to C preprocessor (-I, -D, etc.)
-o directory
Output directory for generated files
-c compiler
Use specific C compiler
--cppopts=options
C preprocessor options
-d type
Dump internal information (trace, genbind, ctrav, chs)
-k
Keep intermediate files
--help
Show help
--version
Show version

WORKFLOW

1. Write .chs file with Haskell code and C hooks2. Run c2hs to generate .hs file3. Compile generated Haskell module

CABAL INTEGRATION

Cabal automatically processes .chs files and runs c2hs with appropriate flags.

INSTALL

sudo apt install c2hs
sudo dnf install c2hs
sudo pacman -S c2hs
sudo zypper install c2hs

CAVEATS

Generated code is BSD licensed. Requires C header files to be accessible. Complex C constructs may need manual binding.

SEE ALSO

ghc(1), cabal(1)