Building error using cmake: cannot find -lpthreads
Asked Answered
S

11

52

I have c++ project that was smoothly running on a given machine, and now I am trying to compile it on another one with the same operating system (Xubuntu 14.04).

I've installed all the dependencies and I'am using cmake to build the project, although it stops with the following error:

Determining if the function pthread_create exists in the pthreads failed with the following output: ... /usr/bin/ld: cannot find -lpthreads

The cmakelists.txt lines that include the compiler flags are as follows:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -lpthread -DNDEBUG -DEIGEN_MPL2_ONLY")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -Wall -lpthread -DEIGEN_MPL2_ONLY")
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -O3 -lpthread -I/usr/include/freetype2 -DNDEBUG -DEIGEN_MPL2_ONLY")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -lpthread -I/usr/include/freetype2 -DEIGEN_MPL2_ONLY")

I have done some research and have already tried the following:

-used -pthread/-threads/-thread/-lpthreads instead of -lpthread, which does not solve the issue and makes the build stop without finding the following package: find_package (Threads)

  • changed the order of -lpthread in the cmakelists line above, which gives the same error
  • used different versions o gcc/g++: tried 4.4, 4.6 and 4.8, without any change
  • created a symbolic link to libpthread.so in /usr/lib/, without any change

I would appreciate some help, since I am already short on ideas on what to try next.

Edit 1

The library is where it should:

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

The pthread_create is also found:

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

I have also verified that both libpthread-stubs0 and libc6-dev are present.

Edit 2

This is part of the FindThreads.cmake file content, located in /usr/share/cmake-2.8/Modules/:

if(CMAKE_HAVE_SPROC_H AND NOT CMAKE_THREAD_PREFER_PTHREAD)
  # We have sproc
  set(CMAKE_USE_SPROC_INIT 1)
else()
  # Do we have pthreads?
  CHECK_INCLUDE_FILES("pthread.h" CMAKE_HAVE_PTHREAD_H)
  if(CMAKE_HAVE_PTHREAD_H)

    #
    # We have pthread.h
    # Let's check for the library now.
    #
    set(CMAKE_HAVE_THREADS_LIBRARY)
    if(NOT THREADS_HAVE_PTHREAD_ARG)
      # Check if pthread functions are in normal C library
      CHECK_SYMBOL_EXISTS(pthread_create pthread.h CMAKE_HAVE_LIBC_CREATE)
      if(CMAKE_HAVE_LIBC_CREATE)
        set(CMAKE_THREAD_LIBS_INIT "")
        set(CMAKE_HAVE_THREADS_LIBRARY 1)
        set(Threads_FOUND TRUE)
      endif()

      if(NOT CMAKE_HAVE_THREADS_LIBRARY)
        # Do we have -lpthreads
        CHECK_LIBRARY_EXISTS(pthreads pthread_create "" CMAKE_HAVE_PTHREADS_CREATE)
        if(CMAKE_HAVE_PTHREADS_CREATE)
          set(CMAKE_THREAD_LIBS_INIT "-lpthreads")
          set(CMAKE_HAVE_THREADS_LIBRARY 1)
          set(Threads_FOUND TRUE)
        endif()

        # Ok, how about -lpthread
        CHECK_LIBRARY_EXISTS(pthread pthread_create "" CMAKE_HAVE_PTHREAD_CREATE)
        if(CMAKE_HAVE_PTHREAD_CREATE)
          set(CMAKE_THREAD_LIBS_INIT "-lpthread")
          set(CMAKE_HAVE_THREADS_LIBRARY 1)
          set(Threads_FOUND TRUE)
        endif()

        if(CMAKE_SYSTEM MATCHES "SunOS.*")
          # On sun also check for -lthread
          CHECK_LIBRARY_EXISTS(thread thr_create "" CMAKE_HAVE_THR_CREATE)
          if(CMAKE_HAVE_THR_CREATE)
            set(CMAKE_THREAD_LIBS_INIT "-lthread")
            set(CMAKE_HAVE_THREADS_LIBRARY 1)
            set(Threads_FOUND TRUE)
          endif()
        endif()
      endif()
    endif()

    if(NOT CMAKE_HAVE_THREADS_LIBRARY)
      # If we did not found -lpthread, -lpthread, or -lthread, look for -pthread
      if("THREADS_HAVE_PTHREAD_ARG" MATCHES "^THREADS_HAVE_PTHREAD_ARG")
        message(STATUS "Check if compiler accepts -pthread")
        try_run(THREADS_PTHREAD_ARG THREADS_HAVE_PTHREAD_ARG
          ${CMAKE_BINARY_DIR}
          ${CMAKE_ROOT}/Modules/CheckForPthreads.c
          CMAKE_FLAGS -DLINK_LIBRARIES:STRING=-pthread
          COMPILE_OUTPUT_VARIABLE OUTPUT)

        if(THREADS_HAVE_PTHREAD_ARG)
          if(THREADS_PTHREAD_ARG STREQUAL "2")
            set(Threads_FOUND TRUE)
            message(STATUS "Check if compiler accepts -pthread - yes")
          else()
            message(STATUS "Check if compiler accepts -pthread - no")
            file(APPEND
              ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
              "Determining if compiler accepts -pthread returned ${THREADS_PTHREAD_ARG} instead of 2. The compiler had the following output:\n${OUTPUT}\n\n")
          endif()
        else()
          message(STATUS "Check if compiler accepts -pthread - no")
          file(APPEND
            ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
            "Determining if compiler accepts -pthread failed with the following output:\n${OUTPUT}\n\n")
        endif()

      endif()

      if(THREADS_HAVE_PTHREAD_ARG)
        set(Threads_FOUND TRUE)
        set(CMAKE_THREAD_LIBS_INIT "-pthread")
      endif()

    endif()
  endif()
endif()

Edit 3

Used a minimal Cmakelists.txt as follows:

cmake_minimum_required (VERSION 2.4)
find_package(Threads)

Which produced the following output:

-- 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 
Spleenful answered 11/8, 2015 at 17:44 Comment(6)
Probably unrelated but don't use -lpthread. Use -pthread, both for compiling and linking.Spradlin
Using -pthread does not solves the problem either.Spleenful
So, did you manage to overcome this issue somehow? I'm facing it right now...Anyplace
@Anyplace He did, see comments below the answer. Sadly, Sapiens helpfully did not mention which package was version-mismatched.Eby
why use -lpthreads not -lpthread?Shrill
In my case, I found this question on StackOverflow and the accepted answer helped. Hint for future readers: check for funny characters in path to project, in my case it was percent sign (%) which broke the build.Transpadane
S
42

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.

Spleenful answered 6/8, 2019 at 8:20 Comment(9)
what version?cmake?Hungnam
This is the correct answer, the whole "pthreads" thing is a red herring! I've expanded the answer and explained where to look for the actual error message.Cates
This situation is in fact quite common, when working with complicated builds. Sometimes in-tree macros are poorly written or attempt to do wild things to "fix" the build, like pulling a newer library version from github, instead of just cleanly failing the build with an error messageCates
Ahh I see, so it gives you a configure error, then tells you to look at the logs, which gives a compile error. I'm so confused why it would do that. But thanks.Determined
I don't see how this answer solves the problem. "Typically you'll either find a very precise actual error message there,..." Even if there might be several possible causes, it does not describe what was the "very precise actual error message" and how it solved the problem in the particular case.Maffa
I also don't understand why this is the accepted answer. How could we solve the issue? I have exactly the same problem as the author of the question.Bouleversement
Thank you !!! Noob cmake user here. Had my entire course grade on the line. Apparently, if blas and lapack are missing, cmake still gives this error.Quesnay
I had the same issue, there was no other error higher up that could explain why it was failing. After a long while trying to debug it, the solution for me was just to delete the build folder entire (not just caches) and rebuild from scratch. I don't understand why it was happening, but I'm just adding this comment in case it helps someone else like me.Montreal
this answer is indeed correct, it is only a suggestion to look for CMakeError log, the real error could be on one of source makefiles where a dependency is missing, which you can install using your package manager.Reliable
F
5

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

Febricity answered 14/11, 2018 at 15:8 Comment(4)
Worked on 19.04 when compiling aom. Thanks!Vaudeville
Ouch. This rather looks like a coincidence, than actual problem solving. The indicated error message stems from a configure check. CMake runs dozends of those, and a failure here is not the reason why the build fails. In your case, it might well be that installing libboost-threads changed the way some CMake configure check proceds, and thus just masks the problem.Cates
moreover note that you reasoning is wrong even on the surface of matters. The Error message quoted in the original question is "/usr/bin/ld: cannot find -lpthreads". Note the trailing "s"! You can not fix that error message by installing a libpthread.so. You would need to install a libpthreads.so !!Cates
lchthyo you are of course right, that is not a reasoning or actual problem solving, just a way to apt -y gets all that is missing to make the job done, with Ubuntu's.Febricity
A
3

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 
Aprylapse answered 11/8, 2015 at 18:8 Comment(5)
I will have to check later if libpthread-stubs0-dev is the exact package I have installed. Although the .a and .so files you referred are already on my system, in that exact location. I created a symbolic link of the .so to usr/lib/ just in case, but it did not made a difference.Spleenful
Can you share the CMake script for find_package(Threads)? There should be a FindThreads.cmake which is throwing that error.Aprylapse
CMake is finding pthread, which means CMake is not producing the error "Determining if the function pthread_create exists in the pthreads failed with the following output: ... /usr/bin/ld: cannot find -lpthreads". What is producing the error? Please share the CMake file which is trying to link to pthreads causing the error. Another way of asking is... do you get the error when running cmake or when executing make?Aprylapse
The problem was happening when running cmake. I have already figured it out, the error was silent and the -lpthreads related error 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: it was a version mismatch.Spleenful
On one of your "note"'s: It's -pthread on gcc, and -pthreads on clang.Culdesac
T
3

This appears to be a long-standing CMake bug. Something else is going wrong, CMake gets confused, and reports this spurious problem instead of the real error.

Look for "thread" in your CMakeLists.txt file and temporarily remove that.

In my case, this immediately pinpointed a library (or rather, its development package) that was missing. Installed it, added it to debian/control's Build-Depends: section, recompiled, everything worked.

Trice answered 15/7, 2018 at 17:50 Comment(2)
No, in the many cases I've seen, this is not a bug. CMake indeed does report the correct error. But then it unfortunately dumps the CMakeError.log, which also contains all the configure checks. Thus: search from the top of the output until finding the first relevant errorCates
I've seen three cases so far where that definitely didn't happen. Yes, the second time I saved the output and searched for the error message that showed up after editing CMakeLists.txt. Guess what wasn't to be found anywhere.Trice
B
3

I had the exact same problem, with the minimal Cmakelists.txt giving me the same output. To solve this, just upgrade cmake to the lastest version (3.15 in my case)

Brister answered 5/8, 2019 at 10:19 Comment(2)
Meh. This is a rather not helpful advice, like "close all windows, reboot, than upgrade your system". ;-) I regularly hit a similar situation when working with complex cmake builds; the right advice is try harder to find the actual error message -- more often than not, it is there, somewhere burried between 1000+ lines of not so helpful other outputCates
My cmake is the latest version, this isn't the issue for me.Determined
A
2

on ubuntu 18.04 I solved as below.

$ sudo apt-get install libboost-all-dev
Arsenopyrite answered 21/6, 2019 at 14:25 Comment(1)
Did not work for me. Pthreads is there, but I get this error anyway.Determined
S
2

I found out what was causing my issue. I initially did it with cmake2, but the project needed cmake3. I changed it to cmake3, but it didn't do a clean build, so some leftover garbage was messing everything up. When I cleaned everything and used cmake3 it worked.

Screeching answered 11/5, 2020 at 5:23 Comment(0)
T
1

I also meet this issue. Exactly the same situation: have pthread lib under /lib/x86..., but the find_package() always gives a "can not find lpthread error". And after some check and consulation to my friend, we find that in my case, I build the cmake from source code and let the cmake link search path be wrong. So we deinstall the self-buid version and reinstall the cmake in a "correct" way by adding the apt source and using apt get install. That solves my issue. Hope this could help guys in the same situation.

Trochilus answered 12/3, 2021 at 11:19 Comment(0)
T
0

Kindly try to install one dependency glibc-static

On Ubuntu you can try apt-get install build-essential

On other linux you may install package similar to glibc-static.

Tetter answered 29/8, 2018 at 4:7 Comment(2)
Downvote. This is grossly misleading and potentially harmful advice. If some CMake build really needs a specific glibc (like static) or pthreads, this is typically indicated clearly. However, in the actual case at hand, the reason is something completely different, and the -lpthreads is just a red herring. please try hard to find the actual error messageCates
This was what I needed, and while the commenter above sound very smart, it turns out that for static builds there are different packages.Vallonia
M
0

This question has many answers; it seems that many obscure issues can cause this bug. The other answers didn't work for me, and though my Output/Error logs were fairly clean, I couldn't find any additional error messages or failures there. So I'll add what I did in case it helps anyone who is in my situation.

In short, my problem was fixed when I deleted the entire build directory and rebuilt from scratch. Deleting only the CMakeCache wasn't enough. If you have this issue, try doing that and see if it works

Montreal answered 18/10, 2022 at 16:18 Comment(0)
J
0

I was facing the similar issue:

*bin/ exists - good.
/bin/ld: cannot find -lrt
/bin/ld: cannot find -lpthread
/bin/ld: cannot find -lm
/bin/ld: cannot find -lpthread
/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status
make: *** [bin/lite-qmi-imashostx86_64] Error 1*

When I checked the library, the .a glibc static files were missing for all the above:

[root@localhost lite-qmi-imsa]# ll /usr/lib64/libpth
libpthread-2.17.so      libpthread_nonshared.a  libpthread.so           
libpthread.so.0         libpth.so.20            libpth.so.20.0.27

After, installing the glibc-static, issue resolved.

yum install glibc-static 

[root@localhost lite-qmi-imsa]# ll /usr/lib64/libpth
libpthread-2.17.so      libpthread_nonshared.a  libpthread.so.0         libpth.so.20.0.27  **libpthread.a**            libpthread.so           libpth.so.20
Jacobina answered 16/4 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.