在MacOSX Lion上编译gcc 4.6.1 C++ 0x线程代码时出错

Per*_*eng 5 c++ macos gcc c++11

编译以下代码时:

#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;
}
Run Code Online (Sandbox Code Playgroud)

使用:

$ g++-4.6.1 -std=c++0x -pthread threading.cpp
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

这是在MacOSX Lion上使用自定义构建的gcc 4.6.1.对gcc 4.6有效的所有其他c ++ 0x特性就像魅力一样.这是MacOSX特定的错误吗?

Ant*_*ams 5

std::thread(以及C++ 11线程库的其余部分)仅适用于gcc 4.6.1支持的某些平台.不幸的是,MacOSX不是那些平台之一.

我的商业Just :: Thread库为32位MacOSX和gcc 4.5提供了C++ 11线程工具,但是还不支持gcc 4.6.


Jon*_*ely 3

请参阅http://gcc.gnu.org/PR50196 - Mac OS X 不支持我们依赖的 pthread 的某些部分。构建最新版本不会有帮助,但它可能会针对 GCC 4.7 进行修复