我正在尝试在 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 许可协议
在 CLion 中,要使用标志 -pthread 进行编译,您应该将以下行添加到 CMakeLists.txt(我已经测试过并且它可以工作):