title: autotools for pthreads not setting correct linker flags
body: <p>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.</p>
  
  <p>I found <a href="http://airtower.wordpress.com/2010/06/14/autotools-and-pthreads/" rel="noreferrer">some references</a> that say use an <a href="http://ac-archive.sourceforge.net/ac-archive/acx_pthread.html" rel="noreferrer"><code>ACX_PTHREAD</code> macro</a>. GNU provides an <a href="http://www.gnu.org/software/autoconf-archive/ax_pthread.html" rel="noreferrer"><code>AX_PTHREAD</code> macro</a>. Both are very similar in concept. But I've tried both (on Ubuntu 13.04 64-bit), and found that they set <code>-pthread</code> in <code>$PTHREAD_CFLAGS</code>, but for some reason they don't set the <code>-lpthread</code> linker flag in <code>$PTHREAD_LIBS</code>.</p>
  
  <p>The build fails. When I run <code>make</code>, I get:</p>
  
  <pre><code>...
  /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
  ...
  </code></pre>
  
  <p>In this case, the <code>./configure</code> step shows:</p>
  
  <pre><code>...
  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
  ...
  </code></pre>
  
  <p>I notice it checks for <code>-lpthreads</code>, but shouldn't it be checking for <code>-lpthread</code>?</p>
  
  <p>I've found that I can use:</p>
  
  <pre><code>AC_CHECK_LIB(pthread, pthread_create, [PTHREAD_LIBS+=-lpthread])
  </code></pre>
  
  <p>and then the build succeeds. But I assume this isn't the best way to make it work on the widest variety of platforms.</p>
  
  <p>I see Ubuntu also has a <a href="http://packages.ubuntu.com/search?keywords=libpthread-stubs0-dev" rel="noreferrer">package <code>libpthread-stubs0-dev</code></a>. But I'm not sure what it's for.</p>
  
  <p>What is the "right way" to use pthreads with autotools?</p>