views:

722

answers:

3

Hello I compiled & installed gcc4.4 using macports.

When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:

 #include <thread>
 ...
  std::thread t(handle);
  t.join();
 ....

The compiler returns:

 cserver.cpp: In member function 'int CServer::run()':
 cserver.cpp:48: error: 'thread' is not a member of 'std'
 cserver.cpp:48: error: expected ';' before 't'
 cserver.cpp:49: error: 't' was not declared in this scope

But std::cout <<... compiles fine..

Can anyone help me?

Thanks!

Luis

+2  A: 

Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.

Tronic
The macro name actually has two underscores at the end, but SO misinterprets it as formatting if I write it correctly.
Tronic
Well pointed out, I removed it, but it still gives me the same error.. I tried with gcc4.4 and gcc4.5 beta... This is frustrating.
luis
fixed the missing underscore problem.
caspin
+2  A: 

gcc does not fully support std::thread yet:

http://gcc.gnu.org/projects/cxx0x.html

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

Use boost::thread in the meantime.

Edit

Although the following compiled and ran fine for me with gcc 4.4.3:

#include <thread>
#include <iostream>

struct F
{
  void operator() () const
  {
    std::cout<<"Printing from another thread"<<std::endl;
  }
};

int main()
{
  F f;
  std::thread t(f);
  t.join();

  return 0;
}

Compiled with

g++ -Wall -g -std=c++0x -pthread main.cpp

Output of a.out:

Printing from another thread

Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?

Artem
I tried your code and I get the same error...Can this be OSX related? Or maybe something that isn't right with the GCC install by MacPorts?
luis
It may be that MacPorts doesn't fully support c++0x functionality? Do you have the output from your gcc configure script? Here's one Mac user for whom configure specified that std::thread is not supported: (http://www.mail-archive.com/[email protected]/msg00973.html)
Artem
A: 

Well I tried on Ubuntu with GCC 4.4.1 and it works like a charm. The problem is Mac OS X specific, now only need to find out why...

luis