FFI Help
Simon Peyton-Jones
simonpj at microsoft.com
Fri Jun 6 09:29:29 EDT 2003
Manuel
Would it be worth mentioning or amplifying this point in the FFI spec,
or perhaps in an accompanying Appendix/Commentary of examples and FAQs?
Else someone else is going to trip over it sooner rather than later.
Simon
| -----Original Message-----
| From: haskell-cafe-admin at haskell.org
[mailto:haskell-cafe-admin at haskell.org] On Behalf Of Glynn
| Clements
| Sent: 04 June 2003 08:32
| To: Matthew Donadio
| Cc: haskell-cafe at haskell.org
| Subject: Re: FFI Help
|
|
| Matthew Donadio wrote:
|
| > I am just starting to experiment with FFI, and am running into a
| > problem. I was to create an FFI to the lgamma(3) found in many of
the
| > newer libm implementations. My code follows the sig.
| >
| > The lgamma function works. The gamma function core dumps (I am
using
| > ghc 5.04.3) on me. gdb reports a SIGSEGV in signgam(), but I'm not
sure
| > why. I believe that I need to use the monad because signgam is only
| > valid after lgamma returns.
| >
| > Does anyone have an idea what I am doing wrong?
| >
| > Thanks.
| >
| > --
| > Matthew Donadio <m.p.donadio at ieee.org>
| >
| > > module Gamma (gamma, lgamma) where
| >
| > > import System.IO.Unsafe
| >
| > > foreign import ccall "math.h lgamma" lgammaC :: Double -> IO
Double
| > > foreign import ccall "math.h signgam" signgamC :: IO Int
|
| signgam is an "int" variable, but this assumes that it is a function
| of type "int signgam(void)".
|
| Write a C wrapper "int get_signgam(void) { return signgam; }" and
| import that.
|
| --
| Glynn Clements <glynn.clements at virgin.net>
| Or alternatively, foreign import the address of the int and read it
| directly with 'peek'.
|
| import Foreign
| ...
| foreign import ccall "math.h &signgam" signgamC :: Ptr Int32
| ...
| gammaIO :: Double -> IO Double
| gammaIO x = do lg <- lgammaC x
| s <- peek signgamC
| return $ fromIntegral s * exp lg
|
|
| Regards,
| Malcolm
More information about the FFI
mailing list