Building error using cmake: cannot find -lpthreads

Edit1:

All references below is for Ubuntu.

Package named libpthread-stubs0 is likely only a stub, so won't have the pthread_create function.

Do you have this?

$ find /lib -name "*pthread*"
/lib/x86_64-linux-gnu/libpthread-2.15.so
/lib/x86_64-linux-gnu/libpthread.so.0

Check for the symbol pthread_create which should exist.

$ nm /lib/x86_64-linux-gnu/libpthread.so.0 | grep "pthread_create"
0000000000008140 t __pthread_create_2_1
0000000000008140 T pthread_create@@GLIBC_2.2.5

If that doesn't work, you may need the dev version of pthread which is in libc6-dev. You can search for the package contents which has libpthread.so in http://packages.ubuntu.com/.

Note: Also, it's failing on -lpthreads. Should it be -lpthread instead (without the s)?

Edit 2

Create a simple CMakeLists.txt with the following and run cmake.

cmake_minimum_required (VERSION 2.8.7)
find_package(Threads)

What's the output? Does it find pthread?

My output is:

-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found.
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE 

The problem was happening when running cmake. Though, in this case cmake was not the problem the error was silent and the -lpthreads related error/warning was the only thing being written to the cmake error log file, although that was not causing any issue. I've done a minimal version of the cmakelists.txt and started testing it line by line until I found which package was causing it to stop: finally I found it was a version mismatch...

Hint: search for the actual error message

Typically you'd look for the last error message. However, this (often useful) strategy in such cases leads astray.

What you are looking at is the CMakeCache.txt, the CMakeOutput.log or the CMakeError.log. How comes? When some of the macros or tests in the configure phase fails, CMake "helpfully" dumps these files to the output. Unfortunately, these files can be thousands of lines long, and typically contain lots of "*** Error: xyz" entries, for various configure checks. The one for "-lpthreads" just accidentally happened to be the last one in the log...

Solution: go through the log from the top, identify the section with the configure checks, find the last configure check prior to the point, where CMake identifies failure and dumps its logs. You might also try so search for the text "Configuring incomplete, errors occurred!"

Typically you'll either find a very precise actual error message there, or at least you find the name / path of the macro or function called last, and this allows you to pinpoint down what actually went wrong.


at an Ubuntu 18.04.1 LTS this installation gave me all the files needed:

apt -y install libboost-tools-dev libboost-thread1.62-dev magics++

/usr/lib/x86_64-linux-gnu/libpthread.a
/usr/lib/x86_64-linux-gnu/libpthread.so
/usr/lib/x86_64-linux-gnu/libpthread_nonshared.a

no more errors "/usr/bin/ld: cannot find -lpthreads" after