【发布时间】:2011-12-01 17:34:56
【问题描述】:
编译以下代码时:
#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;
}
使用:
$ g++-4.6.1 -std=c++0x -pthread threading.cpp
我收到以下错误:
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
这是在 MacOSX Lion 上使用自定义构建的 gcc 4.6.1。对 gcc 4.6 有效的所有其他 c++0x 功能就像一个魅力。这是 MacOSX 特有的错误吗?
【问题讨论】:
-
我不知道发生了什么,但我知道我从不使用 -pthread;相反,我在编译器选项的末尾(!)使用
-lpthread -
它可能仍在
std::tr1中。 4.6 发布时,C++11 尚未正式发布。 -
我在这里可能弄错了,但是 -pthread 不是在 posix 线程中链接吗?这不是您的项目所需要的。
-
忽略,
thread似乎不是在std::tr1开头。我不确定它是否在 4.6 中实现。他们的库支持页面仅适用于 SVN 结帐,而不是特定版本。 gcc.gnu.org/onlinedocs/libstdc++/manual/… -
当我遵循本指南时它在 linux 中工作:blog.scalebit.com/2011/04/… 但我无法让它在 osx lion 中工作。