2010-07-04 87 views
2

我想弄清楚如何使用即将到来的C++版本0x。它应该可以在GCC 4.3+中使用gcc std = gnu ++ 0x选项。如何在Eclipse CDT中使用GCC/G ++编译并运行C++ 0x?

我在Eclipse CDT中使用0x编译的简单线程程序,在std = gnu ++ 0x中添加了Project> properties> C/C++ Build> Settings> Miscellaneous> Other flags。

#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; 
} 

该程序只打印“开始”,并返回0.有谁知道为什么它不运行hello函数线程?

+4

您已经添加了'-pthread'标志,给链接器和编译器? – nos 2010-07-04 17:13:05

+0

不错,向链接器添加-pthread标志并且编译器执行它。非常感谢。 – davidhalldor 2010-07-04 17:17:14

+0

添加此为答案,以便我可以投票! – mmocny 2011-03-03 20:09:41

回答

3

要使用线程,还需要链接到线程库。 如果你还没有做到这一点,将-lpthread添加到您的命令行或在您的情况下其他标志字段。

命令行执行(在Eclipse中的控制台窗口中可见)应该是这样的:

gcc -std=gnu++0x -lpthread <source_file_name>.cc

相关问题