[Haskell-cafe] Haskell plugin dynamic linking issue

Lana Black lanablack at amok.cc
Mon Apr 10 14:16:09 UTC 2017

On 10/04/17 13:50, Niklas Hamb├╝chen wrote:
> 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

Unfortunately, adding rts to extra-libraries section doesn't help. Now I
get a build error from cabal:

    Process exited with code: ExitFailure 1
    Logs have been written to:

    Configuring test-0.1...
    Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2: Missing dependency on a
    * Missing C library: HSrts-ghc8.0.2
    This problem can usually be solved by installing the system package that
    provides this library (you may need the "-dev" version). If the
library is
    already installed but in a non-standard location then you can use
the flags
    --extra-include-dirs= and --extra-lib-dirs= to specify where it is.

Relevant cabal file options:
  extra-lib-dirs:      /usr/lib/ghc-8.0.2/rts
  extra-libraries:     HSrts-ghc8.0.2
  ghc-options:         -Wall -Werror -shared -fPIC -dynamic

Note that this is a library, not an executable as in

More information about the Haskell-Cafe mailing list