Search code examples
haskellffihsc2hsc2hs

Difference between hsc2hs and c2hs?


What is the difference between hsc2hs and c2hs?

I know what hsc2hs is a preprocessor but what does it exactly do?

And c2hs can make Haskell modules from C-code, but do I need hsc2hs for this?


Solution

  • They both have the same function: make it easier to write FFI bindings. You don't need to know about hsc2hs if you chose to use c2hs; they are independent. C2hs is more powerful, but also more complicated: Edward Z. Yang illustrates this point with a nice diagram in his c2hs tutorial:

    When should I use c2hs? There are many Haskell pre-processors; which one should you use? A short (and somewhat inaccurate) way to characterize the above hierarchy is the further down you go, the less boilerplate you have to write and the more documentation you have to read; I have thus heard advice that hsc2hs is what you should use for small FFI projects, while c2hs is more appropriate for the larger ones.

    Things that c2hs supports that hsc2hs does not:

    • Automatic generation of foreign import based on the contents of the C header file
    • Semi-automatic marshalling to and from function calls, and
    • Translation of pointer types and hierarchies into Haskell types.