3

I am trying to compile gcc 4.5.1 for cygwin with support for C++0x threads. However, the resulting gcc does not recognize the -pthread option.

My configure command is:

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

The sample program is:

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

I am compiling a C++ program using

$ 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

My question is how should I configure gcc?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Salil
  • 9,534
  • 9
  • 42
  • 56

2 Answers2

0

I was able to compile your code with g++ with just -pthread and -std=c++0x flags alone or with gcc using the previous flags plus -lstdc++.

However, when I used your flags, it did not work (the error was quite different though), so maybe try it with the following flags next time (because it does not have to necessarily be caused (only) by you compiling GCC with wrong config).

gcc -lstdc++ -std=c++0x -pthread your.cpp
g++ -std=c++0x -pthread your.cpp
Palmik
  • 2,675
  • 16
  • 13
  • Both options give the same error that -pthread option is unrecognized. I suspect the issue is with cygwin as outlined in: http://stackoverflow.com/questions/3414834/gcc-stdthread-problem – Salil Aug 25 '10 at 04:39
0

As you see in the error message, the problem is not with your configuration, but with your g++ option. Use

g++ -lpthread

for pthreads (POSIX threads) and

g++ -lboost_thread

for boost threads. (-pthread is wrong.)

see the manual of g++

man gcc
Shayan Pooya
  • 1,049
  • 1
  • 13
  • 22
  • This is an uninformed advice. `-pthread` sets preprocessor flags and links against appropriate libraries, whereas `-lpthread` just links. For cygwin the correct option is `-threads` which is used for both compiling and linking. – Maxim Egorushkin Jun 19 '13 at 15:23