这是我的环境:
最近,我开始学习网络编程并下载muduo进行学习。虽然我在构建源代码时遇到问题,但因为cmake会抱怨“找不到-lpthreads”。
我做了一些研究。它主要是由Ubuntu 14.10下的新版gcc引起的。 gcc-4.9将使用“-pthread”链接到pthread库,但是,旧版本的gcc使用“-lpthreads”。似乎cmake仍然使用“-lpthreads”,我不知道如何纠正这个......
以下是错误日志:
File /home/jack/workspace/github/build/release/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/jack/workspace/github/build/release/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTryCompileExec2265723491/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec2265723491.dir/build.make CMakeFiles/cmTryCompileExec2265723491.dir/build
make[1]: Entering directory '/home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp'
/usr/local/bin/cmake -E cmake_progress_report /home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp/CMakeFiles 1
Building C object CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o -c /usr/local/share/cmake-3.1/Modules/CheckFunctionExists.c
Linking C executable cmTryCompileExec2265723491
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/cmTryCompileExec2265723491.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTryCompileExec2265723491.dir/CheckFunctionExists.c.o -o cmTryCompileExec2265723491 -rdynamic -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
CMakeFiles/cmTryCompileExec2265723491.dir/build.make:88: recipe for target 'cmTryCompileExec2265723491' failed
make[1]: Leaving directory '/home/jack/workspace/github/build/release/CMakeFiles/CMakeTmp'
Makefile:118: recipe for target 'cmTryCompileExec2265723491/fast' failed
make[1]: *** [cmTryCompileExec2265723491] Error 1
make: *** [cmTryCompileExec2265723491/fast] Error 2
有人知道如何解决这个问题并让我在Ubuntu 14.10上编译muduo吗?
答案 0 :(得分:4)
设置目标的编译或链接标志:
set_target_properties(target1 PROPERTIES COMPILE_FLAGS -pthread LINK_FLAGS -pthread)
或设置全局变量:
set(CMAKE_LINKER_FLAGS "-pthread" CACHE STRING "Linker Flags" FORCE)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS}" CACHE STRING "" FORCE)
答案 1 :(得分:1)
我刚收到muduo作者的回复。这是由于缺少 libboost-dev 引起的。错误消息具有误导性。
应用以下命令后:
sudo apt-get install g++ libboost-dev cmake make git
构建将成功。
答案 2 :(得分:1)
对于CMake 3.1或更高版本,例如,使用THREADS_PREFER_PTHREAD_FLAG首选-pthread
,
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)