nh2 / static-haskell-nix

easily build most Haskell programs into fully static Linux executables
388 stars 36 forks source link

Can't statically link against `zeromq4-haskell` without modifications #10

Open vaibhavsagar opened 5 years ago

vaibhavsagar commented 5 years ago

I'm trying to statically link a package against zeromq4-haskell, which is special because it links against libzmq, a C++ library. Based on https://stackoverflow.com/a/37643200 I thought I would only have to add -pgmlg++ to the ghc-options section and stdc++ to the extra-libraries section:

diff --git a/zeromq4-haskell.cabal b/zeromq4-haskell.cabal
index cd4ea53..13e0f82 100644
--- a/zeromq4-haskell.cabal
+++ b/zeromq4-haskell.cabal
@@ -40,7 +40,8 @@ source-repository head

 library
     hs-source-dirs:    src
-    ghc-options:       -Wall -O2 -fwarn-tabs -funbox-strict-fields
+    ghc-options:       -Wall -O2 -fwarn-tabs -funbox-strict-fields -pgmlg++
+    extra-libraries: stdc++

     exposed-modules:
         Data.Restricted

But this did not seem to work. After asking on IRC, I tried adding -static-libgcc -static-libstdc++ and this didn't work either. Finally @nh2 found that adding -lstdc++ to the final linker invocation resulted in a working binary, and suggested adding -optl-Wl,--start-group -optl-lstdc++ to my ghc-options. This works, but is not ideal because the consumer of the static library has to know to use this flag instead of the library itself. Is there a better way?

A reproduction is available at https://gist.github.com/vaibhavsagar/219af0ae2ca1e4861806f11de2b62c85 (it uses a slightly different approach for building but the problem and solution are the same).