[Haskell-cafe] FFI and GHCi

Emil Axelsson emax at chalmers.se
Tue Oct 21 12:10:18 EDT 2008


Hi,

I'm making my first attempt at using some C code in my Haskell program. I need 
it because I have a large amount of small constant tables, and GHC takes ages to 
compile the if I use ordinary lists (and the object file gets huge). If there's 
any way of achieving this without going to C, I'd be interested to know.

My question is about how to compile a library that contains C code. At the end 
of this message is a simple example of an 'increase' function. To compile, I run

   ghc Increase.hs --make -o increase increase.c

and everything works as expected. But then when I want to load the example in 
GHCi, I need to give the object file at the command line

   ghci Increase increase.o

or I get "unknown symbol `increase'" when I try to run main. It feels a bit 
awkward to have to list the object files every time I want to run GHCi. Is there 
any way of avoiding that? There must be, because if I install the files as a 
Cabal library, I can fire up GHCi without mentioning any object files. But I 
don't want to go through cabal every time I want test some part of my code.

Thanks for any help,

/ Emil



----------------

increase.h:

   int increase(int x);



increase.c:

   #include "increase.h"

   int increase(int x) {
     return x+1;
   }



Increase.hs:

   {-# INCLUDE "increase.h" #-}
   {-# LANGUAGE ForeignFunctionInterface #-}

   import Foreign.C

   foreign import ccall "increase.h increase" inc :: CInt -> CInt

   main = print (inc 2, inc 20)

----------------




More information about the Haskell-Cafe mailing list