2010-07-04 137 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

相關問題