[C2hs] No automatic support for marshaling of structures?

Manuel M T Chakravarty chak at cse.unsw.edu.au
Fri Aug 4 13:50:16 EDT 2006


Hi Jun,

> When I was trying to marshal a C structure in Haskell, I met such error:
> "Illegal structure or union type! There is no automatic support for
> marshaling of structures and unions". However, if I try to marshal the
> pointer to a structure, it works well.

As the error message says there is no automatic facility for marshalling
structs and unions - in fact, there is no one best way to do it.

The easiest solution is to define a Haskell version of your C structure

> The structure I define in C is:
> -------------
> typedef point{
>  int x,y;
> };
> --------------

So, you would define

  data Point = Point {x :: Int, y :: Int}

Then, define an instance of the type class Storable for Point.
(Storable is a class from the FFI addendum, see the GHC Haddock
documentation for details.)  Afterwards you can use c2hs get and set
hooks to marshal between the C and the Haskell representation.

Manuel




More information about the C2hs mailing list