c++ - 如何让 cmake 使用 "-pthread"而不是 -lpthread”?

标签 c++ ubuntu gcc cmake pthreads

这是我的环境:

  • 操作系统:Ubuntu 14.10
  • 海湾合作委员会:4.9
  • cmake: 2.8, 3.1(都试过了)
  • 项目:muduo

最近开始学习网络编程,下载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 吗?

最佳答案

设置目标的编译或链接标志:

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)

关于c++ - 如何让 cmake 使用 "-pthread"而不是 -lpthread”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28029195/

相关文章:

C++ 模板包扩展/可变参数

找不到 Node.js

c++ - 我可以在不循环遍历的情况下检查数组/vector 中的单词吗?

c++ - 作为非类型模板参数的常量字符串引用

c++ - 为什么 STL ifstream 和 ofstream 类不将 std::string 作为文件名?

ubuntu - Xfce:重新启动 xfce 而不用新的显示配置注销

ubuntu - 虚拟盒子 : Port Forwarding for SSH from Win10 Host to Ubuntu Guest Stopped Working

c++ - 创建静态和共享 C++ 库

c - 合法的数组赋值。是否可以?

c - 链接器无法识别外部语句