How to produce a statically linked binary with GHC?

Nicola Squartini tensor5 at gmail.com
Fri Aug 15 01:02:31 EDT 2008


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/glasgow-haskell-users/attachments/20080814/535513d3/attachment-0001.htm


More information about the Glasgow-haskell-users mailing list