我正在尝试弄清楚如何使用即将发布的 C++ 版本 0x。通过使用 gcc std=gnu++0x 选项,它应该可以在 GCC 4.3+ 中使用。
我使用 0x 的简单线程程序在 Eclipse CDT 中编译,并在“项目”>“属性”>“C/C++ 构建”>“设置”>“杂项”>“其他标志”中添加了 std=gnu++0x。
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout << "Hello Concurrent World!" << endl;
}
int main()
{
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
return 0;
}
程序只打印“starting”并返回0。有谁知道为什么它不运行线程化的hello函数?
最佳答案
要使用线程,您还需要链接线程库。
如果您还没有这样做,请添加 -lpthread
到您的命令行或在您的情况下到其他标志字段。
命令行执行(在 Eclipse 的控制台窗口中可见)应如下所示:
gcc -std=gnu++0x -lpthread <source_file_name>.cc
关于concurrency - 如何在Eclipse CDT中使用GCC/G++编译并运行C++0x?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3175450/