Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

macos

gcc

c++11

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?

like image 427
Per Arneng Avatar asked Oct 05 '11 18:10

Per Arneng


2 Answers

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.

like image 84
Anthony Williams Avatar answered Nov 07 '22 06:11

Anthony Williams


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

like image 3
Jonathan Wakely Avatar answered Nov 07 '22 06:11

Jonathan Wakely