[Haskell] Importing things via the FFI?

Duncan Coutts duncan.coutts at worc.ox.ac.uk
Mon May 29 20:13:07 EDT 2006


On Mon, 2006-05-29 at 20:06 -0400, ihope wrote:
> So I decided to pop into the FFI Report thing. I came up with this C
> file (which may or not actually be valid C; I'm not a C guy), called
> ffitest.h:
> 
> > int add(Int x, Int y) {
> >   return x+y;
> > }
> 
> And ffitest.hs:
> 
> > import Foreign.C.Types
> >
> > foreign import ccall "ffitest.h add" add :: CInt -> CInt -> CInt
> 
> Ye error message:
> 
> > During interactive linking, GHCi couldn't find the following symbol:
> >   add
> > This may be due to you not asking GHCi to load extra object files,
> > archives or DLLs needed by your current session. (blah blah blah)
> 
> Obviously I'm doing something wrong here, but what is it?

You need to load the .o file too:

note that it's not a good idea to have the .c and the .hs file with the
same name since they'll want to make the same .o file. So let's assume
you've got ffifest.hs and cbits.c

gcc -c cbits.c -o sbits.o
ghc -c ffifest.hs
ghci ffifest.hs sbits.o

That should link ok.

Or to make a program:
gcc -c cbits.c -o sbits.o
ghc -c ffifest.hs
ghc ffifest.o sbits.o -o ffitest

Duncan



More information about the Haskell mailing list