Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autotools for pthreads not setting correct linker flags

I am adding some pthreads code into my Linux application that I'm building with autotools. I was getting an error about not linking in libpthreads. So I want to specify the pthreads dependency and compiler/linker flags in autotools.

I found some references that say use an ACX_PTHREAD macro. GNU provides an AX_PTHREAD macro. Both are very similar in concept. But I've tried both (on Ubuntu 13.04 64-bit), and found that they set -pthread in $PTHREAD_CFLAGS, but for some reason they don't set the -lpthread linker flag in $PTHREAD_LIBS.

The build fails. 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 notice it checks for -lpthreads, but shouldn't it be checking for -lpthread?

I've found that I can use:

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

and then the build succeeds. But I assume this isn't the best way to make it work on the widest variety of platforms.

I see Ubuntu also has a package libpthread-stubs0-dev. But I'm not sure what it's for.

What is the "right way" to use pthreads with autotools?

like image 726
Craig McQueen Avatar asked Jun 11 '13 23:06

Craig McQueen


2 Answers

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

Compiler flags and linker flags are not mutually-exclusive sets, not least because linking is typically done via the compiler frontend (cc) and not by invoking the linker (ld) directly. Any flags that you can use in the compile step (e.g. -O2, -DFOO, -I/tmp/include) will generally be accepted in the linking step, even if it's not applicable then. (The reverse may not be true, e.g. -lfoo.)

Given that, it's a lot less error-prone to use PTHREAD_CFLAGS (and other CFLAGS variables) when linking, rather than duplicating the applicable flags into PTHREAD_LIBS/LDFLAGS/etc. variables and not using any CFLAGS variables then.

So just use PTHREAD_CFLAGS for your linker, too.

like image 87
usr1234567 Avatar answered Oct 14 '22 11:10

usr1234567


I bumped into this same issue when I added a first C++ source to an otherwise working C project (a shared library). Adding this C++ file caused libtool to switch from linking with gcc to linking with g++. Seems that linking with gcc a '-pthread' is enough to add the dynamic dependency to libpthread, but when linking with g++, it is not.

I tried with the above patch to a local ax_pthread.m4, but this didn't help. Passing '-lpthread' to g++ would fix the issue.

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

--- 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
like image 23
user2365669 Avatar answered Oct 14 '22 10:10

user2365669