/usr/bin/ld: 找不到 -lpthreads

/usr/bin/ld: cannot find -lpthreads

我正在 Fedora 22 上编译 NVIDIA Caffe 工具,但在查找 lpthread 库时遇到问题:

Determining if the pthread_create exist failed with the following output:
Change Dir: /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/gmake" "cmTC_d410e/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_d410e.dir/build.make CMakeFiles/cmTC_d410e.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o
/usr/bin/cc    -o CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o   -c /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c
Linking C executable cmTC_d410e
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_d410e.dir/link.txt --verbose=1
/usr/bin/cc       CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o  -o cmTC_d410e -rdynamic 
CMakeFiles/cmTC_d410e.dir/CheckSymbolExists.c.o: En la función `main':
CheckSymbolExists.c:(.text+0x16): referencia a `pthread_create' sin definir
collect2: error: ld devolvió el estado de salida 1
CMakeFiles/cmTC_d410e.dir/build.make:97: recipe for target 'cmTC_d410e' failed
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_d410e/fast' failed
gmake[1]: *** [cmTC_d410e] Error 1
gmake: *** [cmTC_d410e/fast] Error 2

File /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp/CheckSymbolExists.c:
/* */
#include <pthread.h>

int main(int argc, char** argv)
{
  (void)argv;
#ifndef pthread_create
  return ((int*)(&pthread_create))[argc];
#else
  (void)argc;
  return 0;
#endif
}

Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/gmake" "cmTC_e693b/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_e693b.dir/build.make CMakeFiles/cmTC_e693b.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o   -c /usr/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTC_e693b
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_e693b.dir/link.txt --verbose=1
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create    CMakeFiles/cmTC_e693b.dir/CheckFunctionExists.c.o  -o cmTC_e693b -rdynamic -lpthreads 
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld outputs 1
CMakeFiles/cmTC_e693b.dir/build.make:97: recipe for target 'cmTC_e693b' failed
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_e693b/fast' failed
gmake[1]: *** [cmTC_e693b] Error 1
gmake: *** [cmTC_e693b/fast] Error 2

它似乎没有找到 pthreads 库 (/usr/bin/ld: cannot find -lpthreads),但是 运行 命令 find / name "*pthread*" 它输出这个:

/usr/lib64/libpthread.so
/usr/lib64/libpthread.a
/usr/lib64/libpthread_nonshared.a
/usr/lib64/libpthread-2.21.so
/usr/lib64/libpthread.so.0

但这有点奇怪,因为 CMakeOutut.log 有这个: 确定函数 pthread_create 是否存在于通过以下输出传递的 pthread 中:

Change Dir: /home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp

Run Build Command:"/usr/bin/gmake" "cmTC_705ba/fast"
/usr/bin/gmake -f CMakeFiles/cmTC_705ba.dir/build.make CMakeFiles/cmTC_705ba.dir/build
gmake[1]: Entering directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create   -o CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o   -c /usr/share/cmake/Modules/CheckFunctionExists.c
Linking C executable cmTC_705ba
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_705ba.dir/link.txt --verbose=1
/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create    CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o  -o cmTC_705ba -rdynamic -lpthread 
gmake[1]: Leaving directory '/home/user1/Sources/caffe/build/CMakeFiles/CMakeTmp'

它有这一行使用 lpthread:

/usr/bin/cc   -DCHECK_FUNCTION_EXISTS=pthread_create    CMakeFiles/cmTC_705ba.dir/CheckFunctionExists.c.o  -o cmTC_705ba -rdynamic -lpthread

这是CMakeList.txt:

cmake_minimum_required(VERSION 2.8.7)

# ---[ Caffe project
project(Caffe C CXX)

# ---[ Caffe version
set(CAFFE_TARGET_VERSION "0.14.5")
set(CAFFE_TARGET_SOVERSION "0.14")
add_definitions(-DCAFFE_VERSION=${CAFFE_TARGET_VERSION})

# ---[ Using cmake scripts and modules
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules)

include(ExternalProject)

include(cmake/Utils.cmake)
include(cmake/Targets.cmake)
include(cmake/Misc.cmake)
include(cmake/Summary.cmake)
include(cmake/ConfigGen.cmake)

# ---[ Options
caffe_option(CPU_ONLY  "Build Caffe without CUDA support" OFF) # TODO: rename to USE_CUDA
caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON IF NOT CPU_ONLY)
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
caffe_option(BUILD_python "Build Python wrapper" ON)
set(python_version "2" CACHE STRING "Specify which Python version to use")
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE)
caffe_option(BUILD_docs   "Build documentation" ON IF UNIX OR APPLE)
caffe_option(BUILD_python_layer "Build the Caffe Python layer" ON)
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
caffe_option(USE_LEVELDB "Build with levelDB" ON)
caffe_option(USE_LMDB "Build with lmdb" ON)
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)

# ---[ Dependencies
include(cmake/Dependencies.cmake)

# ---[ Flags
if(UNIX OR APPLE)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wall")
endif()

if(USE_libstdcpp)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libstdc++")
  message("-- Warning: forcing libstdc++ (controlled by USE_libstdcpp option in cmake)")
endif()

add_definitions(-DGTEST_USE_OWN_TR1_TUPLE)

# ---[ Warnings
caffe_warnings_disable(CMAKE_CXX_FLAGS -Wno-sign-compare -Wno-uninitialized)

# ---[ Config generation
configure_file(cmake/Templates/caffe_config.h.in "${PROJECT_BINARY_DIR}/caffe_config.h")

# ---[ Includes
set(Caffe_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(THIRDPARTY_DIR ${PROJECT_SOURCE_DIR}/3rdparty)
include_directories(${Caffe_INCLUDE_DIR} ${PROJECT_BINARY_DIR} ${THIRDPARTY_DIR})
include_directories(BEFORE src) # This is needed for gtest.

# ---[ Subdirectories
add_subdirectory(src/gtest)
add_subdirectory(src/caffe)
add_subdirectory(tools)
add_subdirectory(examples)
add_subdirectory(python)
add_subdirectory(matlab)
add_subdirectory(docs)

# ---[ Linter target
add_custom_target(lint COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/lint.cmake)

# ---[ pytest target
add_custom_target(pytest COMMAND python${python_version} -m unittest discover -s caffe/test WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/python )
add_dependencies(pytest pycaffe)

# ---[ Configuration summary
caffe_print_configuration_summary()

# ---[ Export configs generation
caffe_generate_export_configs()

我必须安装什么? libphreads 是否存在或我必须使用 libphread?

你打错了。您应该使用 -lpthread 而不是 -lpthreads

或者您可以只使用 -pthread,这也有效。

当我在新 Ubuntu 上关注 caffe 文档时出现此错误,并且在 Google 和 caffe collect2: error: ld returned 1 exit status 之后,我发现我缺少一些包,而不是错字:

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler libatlas-base-dev
sudo apt-get install python-dev python-pip gfortran

更多详细信息,请参考 here

问题是由googletest引起的,设置googletest的CMakeList.txt下面一行即可:

option(gtest_disable_pthreads "Disable uses of pthreads in gtest." ON)

在旗帜上。

原因是:

我们必须检查多个不同的线程库 姓名;顺序非常重要,因为某些系统 (例如 DEC)同时具有 -lpthread-lpthreads,其中一个 库已损坏 (non-POSIX)。