Cabal and c2hs

Dominic Steinitz dominic.steinitz at
Sun May 1 03:09:59 EDT 2005

On Saturday 30 Apr 2005 6:51 pm, Isaac Jones wrote:
> Dominic Steinitz <dominic.steinitz at> writes:
> > Isaac,
> >
> > Thanks for your prompt response. I did have a look at these references
> > before I posted and didn't find them very enlightening.
> Was the example test case I gave you helpful?  

Not really. I needed a simple example like the one I eventually came up with.

> We sorta didn't put the 
> UserHooks in the docs too much because they are a feature that we're
> not as sure about in the long-term; they're trying to support a lot of
> different kinds of things, and it'll take time to make sure we get
> them right.  In the meantime, maybe I'll add a little documentation
> with the warning that things might change.

I thought there already was a warning that things might change.

> > BTW I discovered a bug in the .chs handler. You have "-o " rather than
> > "-o". I can try and send a patch but it might be easier if you edit the
> > file yourself.
> Does "-o " not work?  According to the help output, it's correct:

I don't understand why but it produced a file called " IP_ICMP.hs". If you run 
it at the command line

c2hs -o IP_ICMP.hs ip_icmp.h  IP_ICMP.chs

then you get "IP_ICMP.hs" as required. Perhaps something is taking the extra 
space to be part of the filename?

> Usage: c2hs [ option... ] [header-file] binding-file
>   -C CPPOPTS  --cppopts=CPPOPTS   pass CPPOPTS to the C preprocessor
>   -c CPP      --cpp=CPP           use executable CPP to invoke C
> preprocessor -d TYPE     --dump=TYPE         dump internal information (for
> debugging) -h, -?      --help              brief help (the present message)
> -i INCLUDE  --include=INCLUDE   include paths for .chi files
>   -k          --keep              keep pre-processed C header
>   -o FILE     --output=FILE       output result to FILE (should end in .hs)
>   -v          --version           show version information
>               --old-ffi[=OLDFFI]  use the FFI without `Ptr a'
> > import Distribution.Simple
> > import Distribution.Simple.Utils(rawSystemPath)
> >
> > main =
> >    defaultMainWithHooks
> >       defaultUserHooks {
> >          hookedPreProcessors =
> >             [("chs", \_ _ -> myPpC2hs)]
> >       }
> >
> > myPpC2hs inFile outFile verbose
> >     = rawSystemPath verbose "c2hs" ["-o" ++ outFile, "ip_icmp.h", inFile]
> That looks fine.  You could just use "system" instead of
> rawSystemPath, but since you found it, that's cool.
> I don't use c2hs myself; is there any pattern to the .h files we might
> want to preprocess?  Any way to derive it from the sources or the
> module names?  I'm not sure how better to support the kind of thing
> you need.
I'm not an expert so I really don't know. The author and other users are 
probably your best bet.

My requirement here was to take a system file with a lot of values and use it 
in Haskell. The c file was called ip_icmp.h so I  called the module IP_ICMP. 
The only other file I have used is in.h and I called the the module IN.

I recall that c2hs at that time couldn't swallow netinet/ip_icmp.h and 
netinet/in.h so I copied them and deleted the offending parts (which I didn't 
need). Perhaps the modules should have been called Netinet.ICMP and 
Netinet.IN? I've no idea about Windows. I'm about to recommence work in this 
area so I'll let you know if anything occurs to me.


More information about the Libraries mailing list