Per Arneng
Per Arneng

Reputation: 2118

Error when compiling gcc 4.6.1 C++0x threading code on MacOSX Lion

When compiling the following code:

#include <iostream>
#include <thread>

using namespace std;

void hello()
{
        cout << "Hello World!" << endl;
}

int main()
{
        cout << "starting" << endl;
        thread t(hello);
        t.join();
        cout << "ending" << endl;
        return 0;
}

using:

$ g++-4.6.1 -std=c++0x -pthread threading.cpp

I get the following error:

threading.cc: In function ‘int main()’:
threading.cc:13:2: error: ‘thread’ was not declared in this scope
threading.cc:13:9: error: expected ‘;’ before ‘t’
threading.cc:14:2: error: ‘t’ was not declared in this scope

This is on MacOSX Lion with a custom built gcc 4.6.1. All the other c++0x features that are valid for gcc 4.6 works like a charm. Is this a MacOSX specific error?

Upvotes: 5

Views: 1877

Answers (2)

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

See http://gcc.gnu.org/PR50196 - Mac OS X doesn't support some parts of pthreads that we rely on. Building the latest version won't help, but it might be fixed for GCC 4.7

Upvotes: 3

Anthony Williams
Anthony Williams

Reputation: 68561

std::thread (and the rest of the C++11 thread library) is only available for some of the platforms supported by gcc 4.6.1. Unfortunately for you, MacOSX is not one of those platforms.

My commercial Just::Thread library provides the C++11 thread facilities for 32-bit MacOSX with gcc 4.5, but gcc 4.6 is not supported yet.

Upvotes: 5

Related Questions