/usr/bin/ld: 找不到 -lpthreads
Posted
技术标签:
【中文标题】/usr/bin/ld: 找不到 -lpthreads【英文标题】:/usr/bin/ld: cannot find -lpthreads 【发布时间】:2016-06-22 11:44:42 【问题描述】:我正在 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 lib (/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()
我必须安装什么? libpreads 是否存在或者我必须使用 libpread?
【问题讨论】:
【参考方案1】:你有一个错字。您应该使用-lpthread
而不是-lpthreads
。
您也可以只使用-pthread
,这也可以。
【讨论】:
可能 Caffe 有错字。我只运行cmake ..
那么你应该分享你的cmake
配置文件。其中某处会出现 pthread 开关值的错误定义
它很可能被称为CMakeLists
或CMakeLists.txt
我已经用 CMakeLists.txt 的内容更新了我的问题。
我刚刚在 nvidia/caffe 项目中搜索了pthreads
,但在当前提交中没有找到任何内容。听起来像 reclone 会修正错字。【参考方案2】:
当我在新的 Ubuntu 上关注 caffe 文档时出现此错误,在使用 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。
【讨论】:
【参考方案3】:问题是googletest引起的,只需设置googletest的CMakeList.txt下面一行:
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." ON)
在旗帜上。
原因是:
我们必须检查多个不同的线程库
姓名;排序非常重要,因为有些系统
(例如 DEC)同时具有 -lpthread
和 -lpthreads
,其中之一
库已损坏(非 POSIX)。
【讨论】:
以上是关于/usr/bin/ld: 找不到 -lpthreads的主要内容,如果未能解决你的问题,请参考以下文章