2010-08-24 73 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