threadlib: support static linking
(too old to reply)
Bruno Haible
2017-07-16 13:43:46 UTC
When configuring a gnulib testdir with LDFLAGS="-static", the multithreading
tests fail in strange ways. The reason is that weak symbols don't work:
In a program linked through "gcc -static ... -pthread", symbols declared
weak (such as pthread_cancel) are marked "w" in the resulting executable,
i.e. their value evaluates to NULL. Linking with -pthread or -lpthread was
meant to avoid this, but did not have the desired effect.

2017-07-16 Bruno Haible <***@clisp.org>

threadlib: Support static linking.
* m4/threadlib.m4 (gl_THREADLIB_BODY): When static linking is in use,
set gl_cv_have_weak to 'no'.

diff --git a/m4/threadlib.m4 b/m4/threadlib.m4
index f79fde2..6fd1b9e 100644
--- a/m4/threadlib.m4
+++ b/m4/threadlib.m4
@@ -1,4 +1,4 @@
-# threadlib.m4 serial 12
+# threadlib.m4 serial 13
dnl Copyright (C) 2005-2017 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -148,6 +148,10 @@ int main ()
[gl_cv_have_weak="guessing no"])
+ dnl But when linking statically, weak symbols don't work.
+ case " $LDFLAGS " in
+ *" -static "*) gl_cv_have_weak=no ;;
+ esac
if test "$gl_use_threads" = yes || test "$gl_use_threads" = posix; then
# On OSF/1, the compiler needs the flag -pthread or -D_REENTRANT so that