使用cmake生成错误:找不到-lpthreads [英] Building error using cmake: cannot find -lpthreads

查看:5169
本文介绍了使用cmake生成错误:找不到-lpthreads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在给定机器上顺利运行的c ++项目,现在我正在尝试在具有相同操作系统(Xubuntu 14.04)的另一台计算机上进行编译.

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).

我已经安装了所有依赖项,并且正在使用cmake来构建项目,尽管它会因以下错误而停止:

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

确定函数pthread_create是否存在于pthread中失败,并显示以下输出: ... /usr/bin/ld:找不到-lpthreads

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

包含编译器标志的cmakelists.txt行如下:

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:

-使用-pthread/-threads/-thread/-lpthreads而不是-lpthread,这不能解决问题,并使构建停止而未找到以下软件包: find_package (Threads)

-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)

  • 更改了上面cmakelists行中-lpthread的顺序,这给出了相同的错误
  • 使用了gcc/g ++的不同版本:尝试了4.4、4.6和4.8,没有任何更改
  • 在/usr/lib/中创建了指向libpthread.so的符号链接,无需进行任何更改

我将不胜感激,因为我已经缺少下一步的想法.

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

编辑1

该库应该在其中:

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

还找到了pthread_create:

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

我还验证了 libpthread-stubs0 libc6-dev 都存在.

编辑2

这是 FindThreads.cmake 文件内容的一部分,位于/usr/share/cmake-2.8/Modules/:

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()

编辑3

使用最小的Cmakelists.txt,如下所示:

Used a minimal Cmakelists.txt as follows:

cmake_minimum_required (VERSION 2.4)
find_package(Threads)

产生了以下输出:

-- 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 

推荐答案

运行cmake时发生问题.但是,在这种情况下,cmake并不是问题,该错误是静默的,并且-lpthreads相关的错误/警告是唯一写入cmake错误日志文件的内容,尽管这不会引起任何问题. 我已经完成了cmakelists.txt的最低版本,并开始逐行对其进行测试,直到找到导致该软件包停止的软件包:最终我发现这是一个版本不匹配...

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...

通常,您会查找最后一条错误消息..但是,在这种情况下,这种(通常有用的)策略会误入歧途.

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

您正在查看的是CMakeCache.txtCMakeOutput.logCMakeError.log. 怎么回事?当配置阶段中的某些宏或测试失败时,CMake会有帮助地"将这些文件转储到输出中.不幸的是,这些文件可能长达数千行,并且通常包含许多用于各种配置检查的"*** Error: xyz""条目."-lpthreads"的其中一个恰巧是日志中的最后一个...

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...

解决方案:浏览 top 中的日志,标识具有配置检查的部分,找到最后的配置检查 CMake识别故障并转储其日志的位置.您也可以尝试搜索文本"Configuring incomplete, errors occurred!"

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!"

通常,您会在那里找到非常精确的实际错误消息,或者至少找到了名为last的宏或函数的名称/路径,这使您可以查明实​​际出了什么问题.

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.

这篇关于使用cmake生成错误:找不到-lpthreads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆