Autotools for pthreads not setting the correct linker flags

I am adding some pthreads code to my Linux application that I create using autotools. I got an error so as not to get involved with libpthreads. So I want to specify the pthreads dependency and compiler / linker flags in autotools.

I found some links that use the ACX_PTHREAD macro . GNU provides the macro AX_PTHREAD . Both concepts are very similar. But I tried both (on Ubuntu 13.04 64-bit) and found that they set -pthread to $PTHREAD_CFLAGS , but for some reason they did not set the linker -lpthread flag to $PTHREAD_LIBS .

Build failed. When I run make , I get:

 ... /bin/sh ../libtool --tag=CXX --mode=link g++ -g -O2 -o myapp main.o ... -lconfuse -llog4cpp -lnsl -lpopt -lfuse -L/usr/local/lib -lrt libtool: link: g++ -g -O2 -o .libs/myapp main.o ... -lconfuse -llog4cpp -lnsl /usr/lib/x86_64-linux-gnu/libpopt.so -lfuse -L/usr/local/lib -lrt /usr/bin/ld: app-fuse.o: undefined reference to symbol ' pthread_kill@ @GLIBC_2.2.5' /usr/bin/ld: note: ' pthread_kill@ @GLIBC_2.2.5' is defined in DSO /lib/x86_64-linux-gnu/libpthread.so.0 so try adding it to the linker command line /lib/x86_64-linux-gnu/libpthread.so.0: could not read symbols: Invalid operation collect2: error: ld returned 1 exit status ... 

In this case, the ./configure step shows:

 ... checking for the pthreads library -lpthreads... no checking whether pthreads work without any flags... no checking whether pthreads work with -Kthread... no checking whether pthreads work with -kthread... no checking for the pthreads library -llthread... no checking whether pthreads work with -pthread... yes checking for joinable pthread attribute... PTHREAD_CREATE_JOINABLE checking if more special flags are required for pthreads... no checking for PTHREAD_PRIO_INHERIT... yes ... 

I noticed that it checks for the presence of -lpthreads , but shouldn't it check for -lpthread ?

I found that I can use:

 AC_CHECK_LIB(pthread, pthread_create, [PTHREAD_LIBS+=-lpthread]) 

and then the build succeeds. But I believe that this is not the best way to make it work on a variety of platforms.

I see that Ubuntu also has the package libpthread-stubs0-dev . But I'm not sure what it is for.

What is the β€œright way” to use pthreads with autotools?

+4
source share
5 answers

Thanks to Peter Simons, who asked on the autoconf mailing list, we have the official official answer :

Compiler flags and linker flags are not mutually exclusive sets, not because linking is usually done through the compiler interface (cc) and not directly link to the linker (ld). Any flags that you can use at the compilation stage (for example, -O2, -DFOO, -I / tmp / include) will usually be accepted at the linking stage, even if this is not applicable then. (The converse may not be true, for example, -lfoo.)

Given that it is much less error prone to use PTHREAD_CFLAGS (and other CFLAGS variables) when linking, rather than duplicating, the applicable flags in PTHREAD_LIBS / LDFLAGS / etc. variables, not using any CFLAGS variables.

So just use PTHREAD_CFLAGS for your linker.

+2
source

I ran into the same problem when I added the first C ++ source for another working C project (shared library). Adding this file to C ++ led libtool to switch from a link to gcc to a link from g ++. It seems that linking to gcc a '-pthread' is enough to add a dynamic dependency to libpthread, but when linking to g ++ it is not.

I tried local ax_pthread.m4 with the above patch, but that did not help. Passing '-lpthread' in g ++ will fix the problem.

Edit: for some reason ax_pthread.m4 forces C as a test language, even if AC_LANG is set to C ++. This patch allows me to work:

 --- m4/ax_pthread.m4_orig 2013-06-15 20:03:36.000000000 +0300 +++ m4/ax_pthread.m4 2013-06-15 20:03:51.000000000 +0300 @@ -87,7 +87,6 @@ AU_ALIAS([ACX_PTHREAD], [AX_PTHREAD]) AC_DEFUN([AX_PTHREAD], [ AC_REQUIRE([AC_CANONICAL_HOST]) -AC_LANG_PUSH([C]) ax_pthread_ok=no # We used to check for pthread.h first, but this fails if pthread.h @@ -313,5 +312,4 @@ ax_pthread_ok=no $2 fi -AC_LANG_POP ])dnl AX_PTHREAD 
+1
source

The macro AX_PTHREAD find the compiler -pthread flag and use it. But it seems that for this particular flag, it should also be specified to the linker (this is apparently equivalent to -lpthread in the linker). I changed the macro as follows, so the -pthread flag -pthread also specified as the linker flag:

 diff --git a/m4/ax_pthread.m4 b/m4/ax_pthread.m4 index 6d400ed..f426654 100644 --- a/m4/ax_pthread.m4 +++ b/m4/ax_pthread.m4 @@ -172,6 +172,12 @@ for flag in $ax_pthread_flags; do AC_MSG_CHECKING([whether pthreads work without any flags]) ;; + -pthread) + AC_MSG_CHECKING([whether pthreads work with $flag]) + PTHREAD_CFLAGS="$flag" + PTHREAD_LIBS="$flag" + ;; + -*) AC_MSG_CHECKING([whether pthreads work with $flag]) PTHREAD_CFLAGS="$flag" 

I think I should introduce this to the authors of the macro.

0
source

Turning around the suggestion above ( fooobar.com/questions/1485706 / ... ) with the exact script, this error went after me after updating my PKbuild.sh script with pthread args:

 ./bootstrap && \ CPPFLAGS=" -g3 -Wall -pthread "\ CFLAGS=" -pthread -g3 -Wall "\ LDFLAGS=" -lpthread "\ ./configure --enable-maintainer-mode \ --enable-debug \ --prefix=/usr \ --mandir=/usr/share/man \ --enable-pie \ --prefix=/usr \ --enable-library \ --enable-test \ ......... [and so on] 
0
source

I used advice from another post: autoconf with -pthread

Here they mentioned that you can download this file:

http://svn.sleuthkit.org/repos/sleuthkit/trunk/configure.ac

Put it in your m4 directory.

Then include this in your configure.ac file:

 ACX_PTHREAD 

Finally, add this to your Makefile.am:

 bin_PROGRAMS = main main_SOURCES = main.c main_CFLAGS = $(PTHREAD_CFLAGS) main_LDADD = $(PTHREAD_LIBS) 
0
source

Source: https://habr.com/ru/post/1485706/


All Articles