[Haskell-cafe] [Haskell] Linker flags for foreign export.

Max Bolingbroke batterseapower at hotmail.com
Thu Mar 10 00:23:58 CET 2011

Hi Jason,

>  Following your advice, I was able to get a working main,
>  linking the .o's (no attempt at an SO this time) with GHC.

I haven't tried it, but how about this:
 1. Use ghc to link a standard Haskell executable that requires your
libraries. Run the link step with -v so you can see the linker flags
GHC uses.
 2. Use those link flags to link a .so instead. Importantly, this .so
will have already been linked against the Haskell RTS, so you will be
able to link it into a C program with no further dependencies.

Now, there will be at least one additional complication. Before C can
call into Haskell functions you will need to arrange for hs_init and
hs_add_root to be executed. In order to do this you will probably have
to write an additional C file, bootstrap.c. This should contain an
initialization procedure that calls hs_init and hs_add_root in an
function decorated with the __attribute__((constructor)) GCC
extension. Bootstrap.o should then be statically linked in with the
.so in order to initialise the Haskell RTS when C dynamically links in
that .so.

>  Is there a tutorial I should be following? Well-Typed's blog
>  post on this in the early days of shared object support seemed
>  to be doing what I was doing.

Last time I checked Well-Typed's blog posts were one of the best
sources of information on this rather arcane topic. There is no easy
way to make this work at the moment, AFAIK :-(


More information about the Haskell-Cafe mailing list