Create a statically linked Haskell library on Mac

I use: Mac OS X 10.10, GHC 7.10.2 (Haskell platform), Cabal 1.22

I am trying to create a statically linked library in Haskell. The goal is to provide a library of C-compatible ABIs for cross-platform and cross-language use. It is expected that none of the consumers of this library will be implemented in Haskell, so a C-compatible ABI is crucial, as well as the ease of distribution of the resulting library.

My problem is that none of the FFI tutorials / examples use third-party modules, and you can hardly even mention it. Both of these prerequisites are for me. When I follow a simple FFI tutorial, it works, but as soon as I put in a third-party module, it fails for me.

I created a very simple example describing my problem: https://github.com/tomkludy/ffihell

If you build the library with cabal configure;cabal build, you will find a dist\buildlibrary with a name like libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7(followed by .a, _p.a and -ghc7.10.2.dylib).

Problem 1: the library is called something different every time . How can I make a library name the same every time I create it?

If you then create a C program that consumes it (tryit.c), changing build_c.shit to indicate the library name you need, it works:

> ghc tryit.c -Idist/build -Ldist/build -lHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7 -no-hs-main -o tryit
> ./tryit
foo 4: 5

However, if you uncomment these lines in Foo.hs, forcing it to extract the Data.Text library:

{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
module Foo where

import qualified Data.Text as T

foreign export ccall foo :: Int -> IO Int

foo :: Int -> IO Int
foo n = return $ n + 1

-- UNCOMMENTED BELOW HERE...
foreign export ccall bar :: Int -> IO ()
bar :: Int -> IO ()
bar n = putStrLn $ T.unpack $ T.concat $ ["." :: T.Text | _ <- [1..n]]

Then try building ...

> cabal build
(... no errors ...)
> ghc tryit.c -Idist/build -Ldist/build -lHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7 -no-hs-main -o tryit
Undefined symbols for architecture x86_64:
  "_textzu1l1AN4I48k37RaQ6fm6CEh_DataziText_concat_closure", referenced from:
      _S45B_srt in libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7.a(Foo.o)
  "_textzu1l1AN4I48k37RaQ6fm6CEh_DataziText_concat_info", referenced from:
      _ffihezu70CjWiqse6C2Al3vL5a4k7_Foo_zdfstableZZC0ZZCffihezzu70CjWiqse6C2Al3vL5a4k7ZZCFooZZCbar2_info in libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7.a(Foo.o)
      _c4bD_info in libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7.a(Foo.o)
  "_textzu1l1AN4I48k37RaQ6fm6CEh_DataziTextziShow_unpackCStringzh_closure", referenced from:
      _S45B_srt in libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7.a(Foo.o)
  "_textzu1l1AN4I48k37RaQ6fm6CEh_DataziTextziShow_unpackCStringzh_info", referenced from:
      _ffihezu70CjWiqse6C2Al3vL5a4k7_Foo_zdfstableZZC0ZZCffihezzu70CjWiqse6C2Al3vL5a4k7ZZCFooZZCbar4_info in libHSffihell-0.1.0.0-70CjWiqse6C2Al3vL5a4k7.a(Foo.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Problem 2: Cannot find characters related to a third-party module

, -, , Data.Text.

, , 50 , . , + , , , /make ..

3: clang/gcc

, tryit.c , , , GHC. Haskell, GHC. - Haskell, GHC ? ... , " ", - .

!

, ld -r, , -force_load ( ) -reexport_library ( , GHC). git. build_lib.sh Haskell, build_c.sh C-source, .

№1 №3, . , .

+4
1

, ghc -v , , , gcc , ld -r. link C, GMP, .

+1

All Articles