小编pin*_*oli的帖子

cmake 中 pthread 和 -pthread 选项之间的区别是什么?

环境

ubuntu 16.04、gcc 5.4.0、cmake 3.5.1

  1. target_link_libraries(承诺pthread)
  2. target_link_libraries(承诺-pthread)
  3. target_link_libraries(承诺-lpthread)

有什么区别,哪个更好?


问题

承诺.cpp

std::promise<int> pr;
auto fut = pr.get_future();
pr.set_value(10); // throw std::exception and terminate
Run Code Online (Sandbox Code Playgroud)

CMakeLists.txt

add_executable(promise promise.cpp)
target_link_libraries(promise pthread)
Run Code Online (Sandbox Code Playgroud)

解决方案

稍微修改 CMakeLists.txt。

add_executable(promise promise.cpp)
target_link_libraries(promise -pthread)
Run Code Online (Sandbox Code Playgroud)

我从这里找到了答案。但我不知道为什么?

但是,最好的解决方案是便携。

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
target_link_libraries(promise Threads::Threads)
Run Code Online (Sandbox Code Playgroud)

c++ pthreads cmake

2
推荐指数
1
解决办法
1929
查看次数

标签 统计

c++ ×1

cmake ×1

pthreads ×1