In a Haskell program, what’s the best way to use constants defined in C headers?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For this task, hsc2hs is your friend.
For a simple example, let’s get the value of
INT_MAXfromlimits.h.With hsc2hs, we can
#includeheaders and use the values of constants with the#constdirective.Instead of building by hand, use Cabal:
Notice that even though the name of the main program is
IntMax.hsc, theMain-Isline points toIntMax.hs. When Cabal looks forIntMax.hsbut findsIntMax.hsc, it automatically feeds the latter through hsc2hs as part of the build.Note that you’ll want to break up lines with multiple constants. Say you’re assembling a bitfield to pass to FormatMessage. You’ll want to write it as
Putting them all on one line will result in syntax errors.