24

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?

hammar
  • 138,522
  • 17
  • 304
  • 385
develhevel
  • 3,161
  • 4
  • 21
  • 27

2 Answers2

25

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.
Mikhail Glushenkov
  • 14,928
  • 3
  • 52
  • 65
  • Unfortunately neither tool helps with c++ libraries. – Vagif Verdi May 15 '11 at 17:25
  • @Vagif: As of today, Haskell can't interface with C++. The only way is to create C compatible bindings from C++ and import them. – fuz May 15 '11 at 19:49
  • @Vagif @FUZxxl C++ used to be just a preprocessor for C. I'm not sure what the current state is, but I'd imagine there are still ways of automatically transforming C++ code to C code in a fairly straightforward way. – Dan Burton May 15 '11 at 23:25
  • 2
    @Dan I don't believe that's true anymore. C++ has become a lot more complex, see templates, etc. – Joel Burget May 16 '11 at 01:06
  • 2
    @Dan: it isn't. C does not support function overloading, templates, exceptions, methods... It is always possible though to provide a C-ish interface over a C++ core. – Matthieu M. May 16 '11 at 08:15
13

Mikhail's answer is good, but there's another side. There are also things that hsc2hs provides that c2hs does not, and it may be necessary to use both in conjunction.

Notably, hsc2hs operates by producing a C executable that is run to generate Haskell code, while c2hs parses header files directly. Therefore hsc2hs allows you to access #defines, etc. So while I've found c2hs better for generating bindings and wrappers to bindings as well as "deep" peeks and pokes into complex C structures, it is not good for accessing constants and enumerations, and it only automates mildly the boilerplate for Storable instances. I've found hsc2hs necessary as well, in conjunction with the bindings-dsl package [1], in particular in my case for predefined constants. In one instance, I have one hsc file for an enormous amount of constants, and one chs file for wrapping the functions that use these constants.

[1] http://hackage.haskell.org/package/bindings-DSL

sclv
  • 38,665
  • 7
  • 99
  • 204
  • 3
    This is still true, but the situation for C2HS has improved a little over the last few years. For instance, `enum` support is now quite a bit better, you can access the values of `#defines`, there's some more marshalling support, there's better support for foreign pointers, and so on. (I do still tend to write a mixture of C2HS and plain FFI code when I have to wrap C libraries, because there are plenty of things that C2HS can't do.) – Ian Ross Mar 08 '15 at 10:39