[Haskell-beginners] Compiling shared (dll) library
Daniel Fischer
daniel.is.fischer at googlemail.com
Mon Dec 5 15:19:30 CET 2011
On Monday 05 December 2011, 13:44:25, Alexander.Vladislav.Popov wrote:
> > Why the separate compilation? Can't you compile them in one go?
>
> Sorry for a stupid question, how is it?
Not stupid at all.
I'm not an expert in matters FFI, so I don't know if it works in all
situations, but for simple cases at least, you can compile in one go
listing .hs and .c files on the command line.
For a foreign import:
// hsFibo.hs:
{-# LANGUAGE ForeignFunctionInterface #-}
module Main (main) where
import System.Environment (getArgs)
foreign import ccall unsafe "fibo"
c_fibo :: Int -> Int
main :: IO ()
main = do
args <- getArgs
let n = case args of
(a:_) -> read a
_ -> 14
print (c_fibo n)
// Fibonacci.c
int fibo(int n){
int a = (n&1) ? 0 : 1, b = (n&1) ? 1 : 0;
while(n > 1){
a += b;
b += a;
n -= 2;
}
return b;
}
$ ghc -O2 hsFibo.hs Fibonacci.c
[1 of 1] Compiling Main ( hsFibo.hs, hsFibo.o )
Linking hsFibo ...
$ ./hsFibo 21
10946
For a foreign export it's a bit more complicated, as you would have to
generate the .h file(s) first by other means (if all your exported
functions are compatible with the implicit types for C functions [pre C99,
iirc], you can get away without the header, but you'll get warnings about
implicit declarations). But with appropriate headers, you can compile in
one go,
$ ghc -O2 -shared -dynamic -fPIC Export.hs useExport.c -o theexport.so
(if you use a C main to create an executable, also pass -no-hs-main).
>
> And one more question, please: how I can export genexPure :: [String] ->
> [String] to achieve all preferences of lazy list? Not all array at once,
> but to get someting like an iterator, what I can call to get new genex.
Pass. I have no idea how to do that.
More information about the Beginners
mailing list