2010-08-24 74 views
3

我想爲支持C++ 0x線程的cygwin編譯gcc 4.5.1。 但是,生成的gcc無法識別-pthread選項。gcc 4.5.1 C++ 0x線程支持的配置選項

我的configure命令是:

./configure --enable-bootstrap --enable-shared --enable-shared-libgcc 
      --with-gnu-ld --enable-languages=c,c++ --enable-libgomp 
      --enable-libssp --enable-threads=posix --with-__thread 

的示例程序是:

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

我編譯使用

$ gcc -Wall -g -std=c++0x -pthread Trial.cpp 
gcc: unrecognized option '-pthread' 
Trial.cpp: In function `int main()': 
Trial.cpp:21:5: error: `thread' was not declared in this scope 
Trial.cpp:21:12: error: expected `;' before `t' 
Trial.cpp:22:5: error: `t' was not declared in this scope 

C++程序我的問題是我應該如何配置GCC ?

+0

你確定你使用的是正確地認識GCC的手冊?我對此不太瞭解,但有幾件事情看起來不對。 – leppie 2010-08-24 05:06:03

+0

是的,我應該使用g ++。但它仍然不起作用。 – Salil 2010-08-25 03:56:31

回答

0

正如您在錯誤消息中看到的,問題不在於您的配置,而在於您的g ++選項。使用

g++ -lpthread 

的並行線程(POSIX線程)和

g++ -lboost_thread 

升壓線程。 (-pthread是錯誤的。)

參見g ++

man gcc 
+0

這是一個不知情的建議。 '-pthread'對相應的庫設置預處理器標誌和鏈接,而'-lpthread'只是鏈接。對於cygwin,正確的選項是'-threads',它用於編譯和鏈接。 – 2013-06-19 15:23:35

+0

糾錯:'-mthreads'。 – 2013-06-19 15:37:24

0

我可以只-pthread和單獨或與gcc-std=c++0x標誌使用以前的標誌加-lstdc++g++編譯代碼。但是,當我使用你的標誌時,它不起作用(儘管這個錯誤很不同),所以下次可能會試着用下面的標誌(因爲它不一定是由你引起的(只有)用錯誤的配置編譯GCC)。

gcc -lstdc++ -std=c++0x -pthread your.cpp 
g++ -std=c++0x -pthread your.cpp 
+0

這兩個選項都給出了與pthread選項無法識別相同的錯誤。 我懷疑問題是與cygwin概述如下: http://stackoverflow.com/questions/3414834/gcc-stdthread-problem – Salil 2010-08-25 04:39:58