[Haskell-cafe] Creating static libraries using GHC

SevenThunders mattcbro at earthlink.net
Fri Mar 16 20:55:14 EDT 2007

Simon Marlow wrote:
> Tom Hawkins wrote:
>> I have a chunk of Haskell code I would like wrap up and distribute as
>> a library.  Is there a way to build a static library (*.a) that
>> includes my code plus the Haskell runtime, which C programs can easily
>> link against?  Here is what I have tried so far...
>> ghc --make -fffi MyLib   # Builds MyLib.o and MyLib_stub.o.
>> gcc -c -I/usr/local/lib/ghc-6.4.1/include MyLibWrapper.c
>> ar -r libMyLib.a MyLib.o MyLib_stub.o MyLibWrapper.o
>> This works fine when I use ghc to compile and link a C program...
>> ghc main.c libMyLib.a
>> But if I use gcc, it throws a lot of unresolved references.  I have no
>> problem compiling with ghc, but the folks using my library probably
>> won't have it installed.
>> I added -v to ghc to see how it calls gcc; it seems to link in
>> different libraries based on what Haskell libraries are being used,
>> and it undefines a bunch is symbol references.  Are there any switches
>> to have ghc return a single archive with everything included?
> No switches, I'm afraid.  You'll need to include the complete contents 
> of libHSrts.a, and whatever packages you're using: probably at least 
> base and haskell98, i.e. libHSbase.a and libHShasekll98.a respectively. 
>   I don't know if you can include the contents of a .a file directly 
> when building another .a file, if not you'll need to unpack these 
> archives and include their contents when building your archive.
> Cheers,
> 	Simon
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

Just to revisit this topic for a second.  I have also attempted to create
static link libraries in linux that can be linked outside of ghc and have
unfortunately failed.  One of the problems is that it is not easily
determined which libraries contain a given unreferenced symbol, moreover
trying to link against all the libraries or all the object code invariably
runs into errors.  For example libHSrts.o contains main which conflicts with
an external main (in say C).  When I attempt to find a minimal set of
libraries to include I invariably run into hundres of unresolved references
such as

 undefined reference to `stg_ap_p_fast'
libPowCDF.a(Parsefile.o): In function `r1IF_info':
libPowCDF.a(HSrts.o): In function `addDLL':
(.text+0xb318): undefined reference to `dlopen'

but there are typically hundreds of such unresolved references.

What I'd really like is something like the --mk-dll option for ghc under
windows to make a stand-alone library in linux, either static or dynamic. 
Otherwise to export Haskell code to the rest of the world requires that the
linking be performed with ghc.  For a complex project that relies heavily on
a given set of compiler tools, this may be impossible.

View this message in context: http://www.nabble.com/Creating-static-libraries-using-GHC-tf941394.html#a9525110
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list