best way to deal with #defines when developing ffi's

Hi all, I'm working on an FFI and as usual there are lots of defines in the header files. What's the best way to make these available to the haskell code ? Ideally it could be done automagically, or at least pseudo-magically so that keeping up with changes to the .h wouldn't be too painful. I'm talking about simple constant defines, nothing tricky like structures or similar. Thanks, Brian

On Tue, Feb 22, 2011 at 9:47 PM,
Hi all,
I'm working on an FFI and as usual there are lots of defines in the header files.
What's the best way to make these available to the haskell code ? Ideally it could be done automagically, or at least pseudo-magically so that keeping up with changes to the .h wouldn't be too painful.
I'm talking about simple constant defines, nothing tricky like structures or similar.
If you use hsc2hs you should be able to refer to the C defines in your Haskell code. Like so #const SO_REUSEPORT where SO_REUSEPORT is a C define. Johan

On 23 February 2011 06:47,
Hi all,
I'm working on an FFI and as usual there are lots of defines in the header files.
What's the best way to make these available to the haskell code ? Ideally it could be done automagically, or at least pseudo-magically so that keeping up with changes to the .h wouldn't be too painful.
I'm talking about simple constant defines, nothing tricky like structures or similar.
If you use hsc2hs you may also like to look at bindings-DSL: http://hackage.haskell.org/package/bindings-DSL It supports integer, floating point, pointer and string typed constants: https://bitbucket.org/mauricio/bindings-dsl/wiki/DetailedUsageGuide/Predefin... Regards, Bas

When using c2hs, you can use the inline header commands to define global variables: ------------------------------------------------------------------------------------------------------ #c const int HSMYLIBRARY_SOME_VAR = C_MY_VAR; const char *HSMYLIBRARY_OTHER_VAR = C_OTHER_VAR; #endc ------------------------------------------------------------------------------------------------------ And then just bind them with foreign imports like any other variable: ------------------------------------------------------------------------------------------------------ foreign import ccall "HSMYLIBRARY_SOME_VAR" someVar :: CInt foreign import ccall "HSMYLIBRARY_OTHER_VAR" otherVarPtr :: Ptr CChar otherVar :: String otherVar = unsafePerformIO (peekCString otherVarPtr) ------------------------------------------------------------------------------------------------------
participants (4)
-
Bas van Dijk
-
briand@aracnet.com
-
Johan Tibell
-
John Millikin