I've written a few FFI bindings that I use for my games:
cute-c2-hsandcute-sound-hs- These are bindings to single-header C libraries. The header itself is included in the git repo.sdl-gpu-hs- This C/C++ library is not included in the repo. It is declared as a dependency viapkgconfig-dependsin the.cabalfile.
Some general tips I've picked up:
hsc2hsworks great. It doesn't do much for you, but it give you a nice way to create safe bindings.- Try to make your binding as "literal" as possible. You will end up with a lot of boilerplate, but it' a good starting point.
- You can embed any C function in Haskell with basically 0 overhead by using raw
Ptrs and primitives.
- You can embed any C function in Haskell with basically 0 overhead by using raw
- After you have the verbatim C binding, you can build higher-level bindings. I like to make "simple" bindings next - they hide the
Ptrallocation andpoke-ing with a litt