[Haskell-cafe] best way to deal with #defines when developing ffi's

John Millikin jmillikin at gmail.com
Sat Feb 26 03:56:20 CET 2011


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)
------------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110225/b1c8fb18/attachment.htm>


More information about the Haskell-Cafe mailing list