1 # This was retrieved from
2 # http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/common/acx_pthread.m4?rev=1227
3 # See also (perhaps for new versions?)
4 # http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/common/acx_pthread.m4
6 dnl @synopsis ACX_PTHREAD([ACTION-IF-FOUND[, ACTION-IF-NOT-FOUND]])
8 dnl @summary figure out how to build C programs using POSIX threads
10 dnl This macro figures out how to build C programs using POSIX threads.
11 dnl It sets the PTHREAD_LIBS output variable to the threads library and
12 dnl linker flags, and the PTHREAD_CFLAGS output variable to any special
13 dnl C compiler flags that are needed. (The user can also force certain
14 dnl compiler flags/libs to be tested by setting these environment
17 dnl Also sets PTHREAD_CC to any special C compiler that is needed for
18 dnl multi-threaded programs (defaults to the value of CC otherwise).
19 dnl (This is necessary on AIX to use the special cc_r compiler alias.)
21 dnl NOTE: You are assumed to not only compile your program with these
22 dnl flags, but also link it with them as well. e.g. you should link
23 dnl with $PTHREAD_CC $CFLAGS $PTHREAD_CFLAGS $LDFLAGS ... $PTHREAD_LIBS
26 dnl If you are only building threads programs, you may wish to use
27 dnl these variables in your default LIBS, CFLAGS, and CC:
29 dnl LIBS="$PTHREAD_LIBS $LIBS"
30 dnl CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
33 dnl In addition, if the PTHREAD_CREATE_JOINABLE thread-attribute
34 dnl constant has a nonstandard name, defines PTHREAD_CREATE_JOINABLE to
35 dnl that name (e.g. PTHREAD_CREATE_UNDETACHED on AIX).
37 dnl ACTION-IF-FOUND is a list of shell commands to run if a threads
38 dnl library is found, and ACTION-IF-NOT-FOUND is a list of commands to
39 dnl run it if it is not found. If ACTION-IF-FOUND is not specified, the
40 dnl default action will define HAVE_PTHREAD.
42 dnl Please let the authors know if this macro fails on any platform, or
43 dnl if you have any other suggestions or comments. This macro was based
44 dnl on work by SGJ on autoconf scripts for FFTW (www.fftw.org) (with
45 dnl help from M. Frigo), as well as ac_pthread and hb_pthread macros
46 dnl posted by Alejandro Forero Cuervo to the autoconf macro repository.
47 dnl We are also grateful for the helpful feedback of numerous users.
49 dnl @category InstalledPackages
50 dnl @author Steven G. Johnson <stevenj@alum.mit.edu>
51 dnl @version 2006-05-29
52 dnl @license GPLWithACException
54 dnl Checks for GCC shared/pthread inconsistency based on work by
55 dnl Marcin Owsiany <marcin@owsiany.pl>
58 AC_DEFUN([ACX_PTHREAD], [
59 AC_REQUIRE([AC_CANONICAL_HOST])
64 # We used to check for pthread.h first, but this fails if pthread.h
65 # requires special compiler flags (e.g. on True64 or Sequent).
66 # It gets checked for in the link test anyway.
68 # First of all, check if the user has set any of the PTHREAD_LIBS,
69 # etcetera environment variables, and if threads linking works using
71 if test x"$PTHREAD_LIBS$PTHREAD_CFLAGS" != x; then
73 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
75 LIBS="$PTHREAD_LIBS $LIBS"
76 AC_MSG_CHECKING([for pthread_join in LIBS=$PTHREAD_LIBS with CFLAGS=$PTHREAD_CFLAGS])
77 AC_TRY_LINK_FUNC(pthread_join, acx_pthread_ok=yes)
78 AC_MSG_RESULT($acx_pthread_ok)
79 if test x"$acx_pthread_ok" = xno; then
87 # We must check for the threads library under a number of different
88 # names; the ordering is very important because some systems
89 # (e.g. DEC) have both -lpthread and -lpthreads, where one of the
90 # libraries is broken (non-POSIX).
92 # Create a list of thread flags to try. Items starting with a "-" are
93 # C compiler flags, and other items are library names, except for "none"
94 # which indicates that we try without any flags at all, and "pthread-config"
95 # which is a program returning the flags for the Pth emulation library.
97 acx_pthread_flags="pthreads none -Kthread -kthread lthread -pthread -pthreads -mthreads pthread --thread-safe -mt pthread-config"
99 # The ordering *is* (sometimes) important. Some notes on the
100 # individual items follow:
102 # pthreads: AIX (must check this before -lpthread)
103 # none: in case threads are in libc; should be tried before -Kthread and
104 # other compiler flags to prevent continual compiler warnings
105 # -Kthread: Sequent (threads in libc, but -Kthread needed for pthread.h)
106 # -kthread: FreeBSD kernel threads (preferred to -pthread since SMP-able)
107 # lthread: LinuxThreads port on FreeBSD (also preferred to -pthread)
108 # -pthread: Linux/gcc (kernel threads), BSD/gcc (userland threads)
109 # -pthreads: Solaris/gcc
110 # -mthreads: Mingw32/gcc, Lynx/gcc
111 # -mt: Sun Workshop C (may only link SunOS threads [-lthread], but it
112 # doesn't hurt to check since this sometimes defines pthreads too;
113 # also defines -D_REENTRANT)
114 # ... -mt is also the pthreads flag for HP/aCC
115 # pthread: Linux, etcetera
116 # --thread-safe: KAI C++
117 # pthread-config: use pthread-config program (for GNU Pth library)
119 case "${host_cpu}-${host_os}" in
122 # On Solaris (at least, for some versions), libc contains stubbed
123 # (non-functional) versions of the pthreads routines, so link-based
124 # tests will erroneously succeed. (We need to link with -pthreads/-mt/
125 # -lpthread.) (The stubs are missing pthread_cleanup_push, or rather
126 # a function called by this macro, so we could check for that, but
127 # who knows whether they'll stub that too in a future libc.) So,
128 # we'll just look for -pthreads and -lpthread first:
130 acx_pthread_flags="-pthreads pthread -mt -pthread $acx_pthread_flags"
134 if test x"$acx_pthread_ok" = xno; then
135 for flag in $acx_pthread_flags; do
139 AC_MSG_CHECKING([whether pthreads work without any flags])
143 AC_MSG_CHECKING([whether pthreads work with $flag])
144 PTHREAD_CFLAGS="$flag"
148 AC_CHECK_PROG(acx_pthread_config, pthread-config, yes, no)
149 if test x"$acx_pthread_config" = xno; then continue; fi
150 PTHREAD_CFLAGS="`pthread-config --cflags`"
151 PTHREAD_LIBS="`pthread-config --ldflags` `pthread-config --libs`"
155 AC_MSG_CHECKING([for the pthreads library -l$flag])
156 PTHREAD_LIBS="-l$flag"
161 save_CFLAGS="$CFLAGS"
162 LIBS="$PTHREAD_LIBS $LIBS"
163 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
165 # Check for various functions. We must include pthread.h,
166 # since some functions may be macros. (On the Sequent, we
167 # need a special flag -Kthread to make this header compile.)
168 # We check for pthread_join because it is in -lpthread on IRIX
169 # while pthread_create is in libc. We check for pthread_attr_init
170 # due to DEC craziness with -lpthreads. We check for
171 # pthread_cleanup_push because it is one of the few pthread
172 # functions on Solaris that doesn't have a non-functional libc stub.
173 # We try pthread_create on general principles.
174 AC_TRY_LINK([#include <pthread.h>],
175 [pthread_t th; pthread_join(th, 0);
176 pthread_attr_init(0); pthread_cleanup_push(0, 0);
177 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
178 [acx_pthread_ok=yes])
181 CFLAGS="$save_CFLAGS"
183 AC_MSG_RESULT($acx_pthread_ok)
184 if test "x$acx_pthread_ok" = xyes; then
193 # Various other checks:
194 if test "x$acx_pthread_ok" = xyes; then
196 LIBS="$PTHREAD_LIBS $LIBS"
197 save_CFLAGS="$CFLAGS"
198 CFLAGS="$CFLAGS $PTHREAD_CFLAGS"
200 # Detect AIX lossage: JOINABLE attribute is called UNDETACHED.
201 AC_MSG_CHECKING([for joinable pthread attribute])
203 for attr in PTHREAD_CREATE_JOINABLE PTHREAD_CREATE_UNDETACHED; do
204 AC_TRY_LINK([#include <pthread.h>], [int attr=$attr; return attr;],
205 [attr_name=$attr; break])
207 AC_MSG_RESULT($attr_name)
208 if test "$attr_name" != PTHREAD_CREATE_JOINABLE; then
209 AC_DEFINE_UNQUOTED(PTHREAD_CREATE_JOINABLE, $attr_name,
210 [Define to necessary symbol if this constant
211 uses a non-standard name on your system.])
214 AC_MSG_CHECKING([if more special flags are required for pthreads])
216 case "${host_cpu}-${host_os}" in
217 *-aix* | *-freebsd* | *-darwin*) flag="-D_THREAD_SAFE";;
218 *solaris* | *-osf* | *-hpux*) flag="-D_REENTRANT";;
220 AC_MSG_RESULT(${flag})
221 if test "x$flag" != xno; then
222 PTHREAD_CFLAGS="$flag $PTHREAD_CFLAGS"
226 CFLAGS="$save_CFLAGS"
227 # More AIX lossage: must compile with xlc_r or cc_r
228 if test x"$GCC" != xyes; then
229 AC_CHECK_PROGS(PTHREAD_CC, xlc_r cc_r, ${CC})
234 # The next part tries to detect GCC inconsistency with -shared on some
235 # architectures and systems. The problem is that in certain
236 # configurations, when -shared is specified, GCC "forgets" to
237 # internally use various flags which are still necessary.
239 AC_MSG_CHECKING([whether to check for GCC pthread/shared inconsistencies])
240 check_inconsistencies=yes
241 case "${host_cpu}-${host_os}" in
242 *-darwin*) check_inconsistencies=no ;;
244 if test x"$GCC" != xyes -o "x$check_inconsistencies" != xyes ; then
249 # In order not to create several levels of indentation, we test
250 # the value of "$ok" until we find out the cure or run out of
257 save_CFLAGS="$CFLAGS"
260 # Try with the flags determined by the earlier checks.
262 # -Wl,-z,defs forces link-time symbol resolution, so that the
263 # linking checks with -shared actually have any value
265 # FIXME: -fPIC is required for -shared on many architectures,
266 # so we specify it here, but the right way would probably be to
267 # properly detect whether it is actually required.
268 CFLAGS="-shared -fPIC -Wl,-z,defs $CFLAGS $PTHREAD_CFLAGS"
269 LIBS="$PTHREAD_LIBS $LIBS"
272 AC_MSG_CHECKING([whether -pthread is sufficient with -shared])
273 AC_TRY_LINK([#include <pthread.h>],
274 [pthread_t th; pthread_join(th, 0);
275 pthread_attr_init(0); pthread_cleanup_push(0, 0);
276 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
279 if test "x$ok" = xyes; then
286 # Linux gcc on some architectures such as mips/mipsel forgets
289 if test x"$ok" = xno; then
290 AC_MSG_CHECKING([whether -lpthread fixes that])
291 LIBS="-lpthread $PTHREAD_LIBS $save_LIBS"
292 AC_TRY_LINK([#include <pthread.h>],
293 [pthread_t th; pthread_join(th, 0);
294 pthread_attr_init(0); pthread_cleanup_push(0, 0);
295 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
298 if test "x$ok" = xyes; then
300 PTHREAD_LIBS="-lpthread $PTHREAD_LIBS"
306 # FreeBSD 4.10 gcc forgets to use -lc_r instead of -lc
308 if test x"$ok" = xno; then
309 AC_MSG_CHECKING([whether -lc_r fixes that])
310 LIBS="-lc_r $PTHREAD_LIBS $save_LIBS"
311 AC_TRY_LINK([#include <pthread.h>],
312 [pthread_t th; pthread_join(th, 0);
313 pthread_attr_init(0); pthread_cleanup_push(0, 0);
314 pthread_create(0,0,0,0); pthread_cleanup_pop(0); ],
317 if test "x$ok" = xyes; then
319 PTHREAD_LIBS="-lc_r $PTHREAD_LIBS"
324 if test x"$ok" = xno; then
325 # OK, we have run out of ideas
326 AC_MSG_WARN([Impossible to determine how to use pthreads with shared libraries])
328 # so it's not safe to assume that we may use pthreads
332 CFLAGS="$save_CFLAGS"
340 AC_SUBST(PTHREAD_LIBS)
341 AC_SUBST(PTHREAD_CFLAGS)
344 # Finally, execute ACTION-IF-FOUND/ACTION-IF-NOT-FOUND:
345 if test x"$acx_pthread_ok" = xyes; then
346 ifelse([$1],,AC_DEFINE(HAVE_PTHREAD,1,[Define if you have POSIX threads libraries and header files.]),[$1])