【问题标题】:How to let cmake use "-pthread" instead of -lpthread"?如何让cmake使用“-pthread”而不是-lpthread”?
【发布时间】:2015-03-17 17:28:22
【问题描述】:

这是我的环境:

  • 操作系统:Ubuntu 14.10
  • GCC:4.9
  • cmake: 2.8, 3.1(都试过了)
  • 项目: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 吗?

【问题讨论】:

  • 您在CMakeLists.txt 中使用find_package(Threads REQUIRED) 还是什么?你应该显示你的文件..
  • 检查这里,如果有帮助stackoverflow.com/questions/23250863/…
  • 刚刚收到mudu作者的回复。这是因为缺少 libboost-dev。错误信息具有误导性...

标签: c++ ubuntu gcc cmake pthreads


【解决方案1】:

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

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)

【讨论】:

    【解决方案2】:

    刚刚收到木多作者的回复。这是由于缺少 libboost-dev 造成的。错误消息具有误导性。

    应用以下命令后:

    sudo apt-get install g++ libboost-dev cmake make git
    

    构建将成功。

    【讨论】:

    • 我已经安装了所有这些包,问题与安装的包无关。
    【解决方案3】:

    对于 CMake 3.1 或更高版本,例如,使用 THREADS_PREFER_PTHREAD_FLAG 来首选-pthread

    set(THREADS_PREFER_PTHREAD_FLAG ON)
    find_package(Threads REQUIRED)
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 2014-08-14
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      相关资源
      最近更新 更多