
I am trying to statically compile a simple haskell program so I can use it on a Linux computer without haskell and it's associated libraries. Here is a small example program that illustrates my problem:
module Main where import Network.Fancy main = do withDgram (IP "127.0.0.1" 1234) (flip send "Hello network\n")
After a bit of googling, I came to the conclusion that I needed to compile it with "ghc --make -static -optl-static Foo.hs". Using only "-static" or "-optl-static" by themselves did not generate a statically linked binary. But when I compile with both those parameters I get a bunch of linker errors: /home/mightybyte/.cabal/lib/network-fancy-0.1.4/ghc-6.10.4/libHSnetwork-fancy-0.1.4.a(Fancy.o): In function `s6ks_info': (.text+0x3068): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking /usr/lib/ghc-6.10.4/libffi.a(closures.o): In function `init_mparams': (.text+0x3e): undefined reference to `pthread_mutex_lock' /usr/lib/ghc-6.10.4/libffi.a(closures.o): In function `init_mparams': (.text+0x52): undefined reference to `pthread_mutex_unlock' /usr/lib/ghc-6.10.4/libffi.a(closures.o): In function `init_mparams': (.text+0xd3): undefined reference to `pthread_mutex_init' /usr/lib/ghc-6.10.4/libffi.a(closures.o): In function `ffi_closure_free': (.text+0x59c): undefined reference to `pthread_mutex_lock' etc... I've tried this on both Fedora and Arch Linux and I get the same error. Anyone know how to solve this problem?

MightyByte
After a bit of googling, I came to the conclusion that I needed to compile it with "ghc --make -static -optl-static Foo.hs". Using only "-static" or "-optl-static" by themselves did not generate a statically linked binary. But when I compile with both those parameters I get a bunch of linker errors: [..] (.text+0x59c): undefined reference to `pthread_mutex_lock'
For some reason, GHC doesn't link with the pthreads library, so you need to compile (link) with two options: -opt-static -optl-pthread -k -- If I haven't seen further, it is by standing in the footprints of giants

On Tue, Nov 10, 2009 at 4:29 PM, Ketil Malde
MightyByte
writes: After a bit of googling, I came to the conclusion that I needed to compile it with "ghc --make -static -optl-static Foo.hs". Using only "-static" or "-optl-static" by themselves did not generate a statically linked binary. But when I compile with both those parameters I get a bunch of linker errors: [..] (.text+0x59c): undefined reference to `pthread_mutex_lock'
For some reason, GHC doesn't link with the pthreads library, so you need to compile (link) with two options: -opt-static -optl-pthread
Excellent, that appears to have solved my problem. It still gives me the warning: (.text+0x3068): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking But glibc is pretty standard, so I don't think this will be a problem for me. Thanks for the help.

On Wed, Nov 11, 2009 at 3:22 PM, MightyByte
(.text+0x3068): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
But glibc is pretty standard, so I don't think this will be a problem for me. Thanks for the help.
You may have unexpected results. That warning occurs because some of glibc (namely, the getaddrinfo bit) is dynamically linked regardless of what you want; this is apparently to make NSS work, or something along those lines. However, if you then link the rest of glibc statically, you get dependencies between your program and the installed glibc, for internal, unstable APIs. (!) This is generally a Big No. Doing this means your program definitely won't be compatible with older versions of glibc, but it probably wouldn't be either way. However, in this case it also won't be compatible with *newer* versions of glibc. My recommendation would be to take glibc off the list of statically linked libraries. -- Svein Ove Aas

On Thu, Nov 12, 2009 at 8:57 AM, David Virebayre
On Wed, Nov 11, 2009 at 5:44 PM, Svein Ove Aas
wrote: My recommendation would be to take glibc off the list of statically linked libraries.
How do you do that ?
By specifying the entire list manually, and not naming glibc. -- Svein Ove Aas
participants (4)
-
David Virebayre
-
Ketil Malde
-
MightyByte
-
Svein Ove Aas