[Haskell-cafe] FFI and GHCi

Emil Axelsson emax at chalmers.se
Tue Oct 21 13:05:58 EDT 2008


I thought so too, but didn't find anything that seemed to work. One thing that 
perhaps could work would be to set the -l flag from the .ghci file. But when I 
tried giving -lincrease on the command line, apparently GHC expects to find a 
file named libincrease.so, which apparently is not the same as the existing 
increase.o (I tried renaming it :) ).

/ Emil



Corey O'Connor skrev:
> I would think there is a command you can embed in the .ghci file that
> would automate the loading of the object files. But I didn't see one
> on a quick scan of the manual:
> http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-dot-files.html
> 
> -Corey O'Connor
> 
> 
> 
> On Tue, Oct 21, 2008 at 9:10 AM, Emil Axelsson <emax at chalmers.se> wrote:
>> 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)
>>
>> ----------------
>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>


More information about the Haskell-Cafe mailing list