How to produce a statically linked binary with GHC?

It seems that the -static flag serves a different purpose: according to the GHC manual it forces the compiler use the static Haskell libraries, but the resulting binary is still dynamically linked to the C libraries. What is the correct flag to create a static binary? Thanks Best Regards Nicola

tensor5:
It seems that the -static flag serves a different purpose: according to the GHC manual it forces the compiler use the static Haskell libraries, but the resulting binary is still dynamically linked to the C libraries. What is the correct flag to create a static binary? Thanks
Pass -optl-static to GHC. -- Don

I did the following in my project and this is what I get: $> runhaskell Setup configure --user --ghc-option="-optl -static" $> runhaskell Setup build ... ... /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_create.o): In function `timer_create': (.text+0x124): undefined reference to `pthread_once' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_create.o): In function `timer_create': (.text+0x171): undefined reference to `pthread_attr_init' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_create.o): In function `timer_create': (.text+0x1b8): undefined reference to `pthread_attr_setdetachstate' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `__start_helper_thread': (.text+0x3f): undefined reference to `pthread_attr_init' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `__start_helper_thread': (.text+0x4c): undefined reference to `pthread_attr_setstacksize' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `__start_helper_thread': (.text+0x92): undefined reference to `pthread_create' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `__start_helper_thread': (.text+0xc6): undefined reference to `pthread_attr_destroy' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `__start_helper_thread': (.text+0xd4): undefined reference to `pthread_atfork' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `timer_helper_thread': (.text+0x1e1): undefined reference to `pthread_exit' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/../../../../lib/librt.a(timer_routines.o): In function `timer_helper_thread': (.text+0x21b): undefined reference to `pthread_create' /usr/lib/gcc/x86_64-pc-linux-gnu/4.2.1/libgcc_eh.a(unwind-dw2.o): In function `uw_init_context_1': (.text+0x1b65): undefined reference to `pthread_once' collect2: ld returned 1 exit status Basically when GHC calls GCC it forgets to add -lpthread. By the way without --ghc-option="-optl -static" everything works and it produces a nice dynamic binary. Regards Nicola On Tue, 2008-08-12 at 13:21 -0700, Don Stewart wrote:
tensor5:
It seems that the -static flag serves a different purpose: according to the GHC manual it forces the compiler use the static Haskell libraries, but the resulting binary is still dynamically linked to the C libraries. What is the correct flag to create a static binary? Thanks
Pass -optl-static to GHC.
-- Don

On 2008 Aug 15, at 1:02, Nicola Squartini wrote:
I did the following in my project and this is what I get:
$> runhaskell Setup configure --user --ghc-option="-optl -static"
No space between "-optl" and "-static". The "-pthread" is probably being misrecognized as a result of the generated command. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

Same result. When I type $> runhaskell Setup build --verbose=3 I see this ... gcc -v -o dist/build/lambdaInit/lambdaInit dist/build/lambdaInit/lambdaInit-tmp/Main.o -static -L/usr/lib/ghc-6.8.2/lib/hinotify-0.2 -L/usr/lib/ghc-6.8.2/lib/containers-0.1.0.1 -L/home/nicola/ghc//lib/VolumeID-0.1/ghc-6.8.2 -L/home/nicola/ghc/lib/Mount-0.1/ghc-6.8.2 -L/usr/lib/ghc-6.8.2/lib/regex-compat-0.71.0.1 -L/usr/lib/ghc-6.8.2/lib/regex-posix-0.72.0.2 -L/usr/lib/ghc-6.8.2/lib/regex-base-0.72.0.1 -L/usr/lib/ghc-6.8.2/lib/bytestring-0.9.0.1 -L/usr/lib/ghc-6.8.2/lib/array-0.1.0.0 -L/usr/lib/ghc-6.8.2/lib/process-1.0.0.0 -L/usr/lib/ghc-6.8.2/lib/unix-2.3.0.0 -L/usr/lib/ghc-6.8.2/lib/directory-1.0.0.0 -L/usr/lib/ghc-6.8.2/lib/filepath-1.1.0.0 -L/usr/lib/ghc-6.8.2/lib/old-time-1.0.0.0 -L/usr/lib/ghc-6.8.2/lib/old-locale-1.0.0.0 -L/usr/lib/ghc-6.8.2/lib/base-3.0.1.0 -L/usr/lib/ghc-6.8.2 -lHShinotify-0.2 -lHScontainers-0.1.0.1 -lHSVolumeID-0.1 -lvolume_id -lHSMount-0.1 -lHSregex-compat-0.71.0.1 -lHSregex-posix-0.72.0.2 -lHSregex-base-0.72.0.1 -lHSbytestring-0.9.0.1 -lHSarray-0.1.0.0 -lHSprocess-1.0.0.0 -lHSunix-2.3.0.0 -lutil -ldl -lHSdirectory-1.0.0.0 -lHSfilepath-1.1.0.0 -lHSold-time-1.0.0.0 -lHSold-locale-1.0.0.0 -lHSbase-3.0.1.0 -lHSrts -lm -lgmp -ldl -lrt -u base_GHCziBase_Izh_static_info -u base_GHCziBase_Czh_static_info -u base_GHCziFloat_Fzh_static_info -u base_GHCziFloat_Dzh_static_info -u base_GHCziPtr_Ptr_static_info -u base_GHCziWord_Wzh_static_info -u base_GHCziInt_I8zh_static_info -u base_GHCziInt_I16zh_static_info -u base_GHCziInt_I32zh_static_info -u base_GHCziInt_I64zh_static_info -u base_GHCziWord_W8zh_static_info -u base_GHCziWord_W16zh_static_info -u base_GHCziWord_W32zh_static_info -u base_GHCziWord_W64zh_static_info -u base_GHCziStable_StablePtr_static_info -u base_GHCziBase_Izh_con_info -u base_GHCziBase_Czh_con_info -u base_GHCziFloat_Fzh_con_info -u base_GHCziFloat_Dzh_con_info -u base_GHCziPtr_Ptr_con_info -u base_GHCziPtr_FunPtr_con_info -u base_GHCziStable_StablePtr_con_info -u base_GHCziBase_False_closure -u base_GHCziBase_True_closure -u base_GHCziPack_unpackCString_closure -u base_GHCziIOBase_stackOverflow_closure -u base_GHCziIOBase_heapOverflow_closure -u base_GHCziIOBase_NonTermination_closure -u base_GHCziIOBase_BlockedOnDeadMVar_closure -u base_GHCziIOBase_BlockedIndefinitely_closure -u base_GHCziIOBase_Deadlock_closure -u base_GHCziIOBase_NestedAtomically_closure -u base_GHCziWeak_runFinalizzerBatch_closure -u base_GHCziConc_ensureIOManagerIsRunning_closure ... So I run that gcc command manually adding -lpthread at the end of it end it works, I get the static binary. Isn't it strange? It could be a gcc problem. On Thu, Aug 14, 2008 at 10:06 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote:
On 2008 Aug 15, at 1:02, Nicola Squartini wrote:
I did the following in my project and this is what I get:
$> runhaskell Setup configure --user --ghc-option="-optl -static"
No space between "-optl" and "-static". The "-pthread" is probably being misrecognized as a result of the generated command.
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

2008/8/15 Nicola Squartini
So I run that gcc command manually adding -lpthread at the end of it end it works, I get the static binary. Isn't it strange? It could be a gcc problem.
Well, it needn't be that hacky ;) The following command works for me and it does essentially the same thing: ghc -i -static -optl-static -optl-pthread -funbox-strict-fields -fwarn-incomplete-patterns -isrc --make Main -o bin/restyscript Please note that without -optl-pthread option, I'll get exactly the same errors: Linking bin/restyscript ... /usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/librt.a(timer_create.o): In function `timer_create': (.text+0x117): undefined reference to `pthread_once' /usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/librt.a(timer_create.o): In function `timer_create': ... -lpthread needs to be put to the end of the argument list to help gcc or ld solve the symbols properly. Hope this helps ;) -agentzh

It works! This is much easier, thank you
Nicola
On Thu, Aug 14, 2008 at 11:30 PM, Agent Zhang
2008/8/15 Nicola Squartini
So I run that gcc command manually adding -lpthread at the end of it end
it works,
I get the static binary. Isn't it strange? It could be a gcc problem.
Well, it needn't be that hacky ;) The following command works for me and it does essentially the same thing:
ghc -i -static -optl-static -optl-pthread -funbox-strict-fields -fwarn-incomplete-patterns -isrc --make Main -o bin/restyscript
Please note that without -optl-pthread option, I'll get exactly the same errors:
Linking bin/restyscript ... /usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/librt.a(timer_create.o): In function `timer_create': (.text+0x117): undefined reference to `pthread_once' /usr/lib/gcc/i486-linux-gnu/4.2.3/../../../../lib/librt.a(timer_create.o): In function `timer_create': ...
-lpthread needs to be put to the end of the argument list to help gcc or ld solve the symbols properly.
Hope this helps ;)
-agentzh

On Fri, Aug 15, 2008 at 4:20 PM, Peter Gammie
Nicola: I had the same problem using the GHC 6.8.2 Debian package. Igloo promises me that GHC 6.8.3 fixes this problem, but I haven't seen that in the Debian repository yet.
GHC 6.8.3 does not have this problem at least on my machine ;) Regards, -agentzh
participants (5)
-
Agent Zhang
-
Brandon S. Allbery KF8NH
-
Don Stewart
-
Nicola Squartini
-
Peter Gammie