building and analysing C structs with Storable

Henning Thielemann lemming at henning-thielemann.de
Sun Oct 14 15:28:45 EDT 2007


On Sun, 14 Oct 2007, Stefan O'Rear wrote:

> On Sun, Oct 14, 2007 at 09:19:24PM +0200, Henning Thielemann wrote:
>>
>> On Sat, 13 Oct 2007, Adam Langley wrote:
>>
>>> On 10/13/07, Henning Thielemann <lemming at henning-thielemann.de> wrote:
>>>> Are there analogons to ByteString's Put and Get Monads to construct and
>>>> disect binary data in a C struct for exchange with a C program?
>>>
>>> The memory layout of structs depends on the ABI of system. You can
>>> take a guess and write code in the Get and Put monads. Your guess will
>>> probably work pretty well within a single architecture (e.g. x86 or
>>> x86-64).
>>>
>>> However, the portable way to do this is to get the information from
>>> the C compiler, which is the approach that c2hs[1] and friends use. I
>>> would recommend this unless there's some good reason to think
>>> otherwise. (see [2] for the exact hook)
>>>
>>> [1] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/
>>> [2] http://www.cse.unsw.edu.au/~chak/haskell/c2hs/docu/c2hs-2.html#ss2.8
>>
>> I do not quite see the need for an extra tool. I thought it must be
>> possible to ship a Haskell compiler with modules that depend on the
>> system's C compiler, just like the modules that are implemented differently
>> for Windows and Unix. Such modules could provide the functionality to
>> create and inspect C structs for exchange with system libraries.
>
> You say "the system's C compiler" as if there was only one.  It's quite
> common for UNIXoid systems to have several C compilers installed
> simultaneously; and if you use the module corresponding to the wrong
> compiler, you get silent data loss.  I wouldn't risk it.

Do different C compilers on the same platform actually use different 
layouts for structs? If yes, how can I find out, with which compiler a 
library was compiled?


More information about the Libraries mailing list