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?
source share