对 pthread CLion 的未定义引用

新手上路,请多包涵

我正在尝试在 CLion 中运行这个简单的线程 c++ 程序

#include <iostream>
#include <thread>

using namespace std;

//Start of the thread t1
 void hello() {
     cout << "Hello,concurrent world!" << endl; }

 int main() {
     thread t1(hello);     // spawn new thread that calls hello()

     cout << "Concurrency has started!" << endl;
     t1.join();

     cout << "Concurrency completed!";

     return 0;
   }

我的问题是对 pthread 的未定义引用存在错误,我不明白我做错了什么……请注意我在 CLion 上这样做。

原文由 A. Lopez 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 772
1 个回答

在 CLion 中,要使用标志 -pthread 进行编译,您应该将以下行添加到 CMakeLists.txt(我已经测试过并且它可以工作):

 SET(CMAKE_CXX_FLAGS -pthread)

原文由 Le Danh 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题