[Haskell-cafe] Haskell plugin dynamic linking issue
Niklas Hambüchen
mail at nh2.me
Mon Apr 10 13:50:34 UTC 2017
I'm not sure if it's the solution to your problem already, but there's a
Cabal bug that makes it not pass cc-options and ld-options to GHC as
ghc-options correctly:
https://github.com/haskell/cabal/issues/4435
You need to additionally pass these options as `ghc-options: "-optl
-lyourlib"` if you want to be sure.
Further, looking at an example I have around, my cabal `library` output
(say I have mypackage.cabal with a "library" in there, it's called
"dist/build/libHSmypackage-...so") link against things like
libHSghc-prim, but not against the RTS (libHSrts...so). I think this
makes sense, because as far as I can tell, the RTS is only linked in
when you compile the final executable, and which RTS .so file is chosen
depends on whether you link in the standard runtime, the -threaded
runtime, the debug runtime, etc.
So I believe that when loading Haskell from C you will have to dlopen()
the RTS you want explicitly, before loading your own .so file. You
probably also need to load the RTS with the RTLD_GLOBAL dlopen flag so
that when you later load your .so, it can see the RTS symbols.
An alternative is probably to add HSrts-ghc-VERSION to
`extra-libraries`, like I do here:
https://github.com/nh2/call-haskell-from-anything/blob/0ba6737ea17a45e59704dfdc21c971fe10b7d692/detect-ghc-buildinfo.py#L25.
That links it explicitly like you planned to do.
According to
https://github.com/nh2/call-haskell-from-anything/issues/19#issuecomment-263688677
there is now a `foreign-library` stanza in Cabal that is supposed to
free you of manually having to link/load the RTS. I haven't tried it
yet, since at the time that comment was written, it wasn't released yet.
Now it might be.
Detail documentation of that feature:
https://github.com/haskell/cabal/blob/db26fa21cba92097d9fdeb580ddf797c35af8ed0/Cabal/doc/developing-packages.rst#foreign-libraries
Hope this helps!
Niklas
More information about the Haskell-Cafe
mailing list