i need a example how to use hsc2hs, i thought that when i write a header file like:
// foo.h
#define PI 3.14159
typedef struct {
int i1;
int i2;
} foo;
struct foo2 {
int i1;
int i2;
};
int fooFkt(foo f);
and then create a hsc file like:
import Foreign
import Foreign.C
#include "foo.h"
use hsc2hs
:
{-# INCLUDE "foo.h" #-}
{-# LINE 1 "test.hsc" #-}
import Foreign
{-# LINE 2 "test.hsc" #-}
import Foreign.C
{-# LINE 5 "test.hsc" #-}
i dont understand it, i thought hat hsc2hs will import all needed things for me like the PI
who can give me a better example?
It works if you actually use some definitions from
foo.h
:gives
Here's a real world usage example from the network package: https://github.com/haskell/network/blob/master/Network/Socket/ByteString/MsgHdr.hsc
Following RWH ch 17, you need to introduce something that binds the C
PI
value to a symbol in Haskell.For example:
And while we're here, we can also marshal the struct to and from Haskell:
And bind to
fooFkt
function:Done.