std :: this_thread :: sleep_for()和GCC [英] std::this_thread::sleep_for() and GCC
问题描述
当我尝试编译这个简单的程序:
When I try to compile this simple program:
#include<thread>
void f() {
std::this_thread::sleep_for(std::chrono::seconds(3));
}
int main() {
std::thread t(f);
t.join();
}
with gcc version 4.4.3 on Ubuntu 10.04 (32 bit):
$ g++ -std=c++0x -pthread a.cpp -o a
我得到:
error: ‘sleep_for’ is not a member of ‘std::this_thread’
我在标题'thread'
sleep_for()受_GLIBCXX_USE_NANOSLEEP保护
I looked in header 'thread'.
sleep_for() is protected with _GLIBCXX_USE_NANOSLEEP
#ifdef _GLIBCXX_USE_NANOSLEEP
...
/// sleep_for
template<typename _Rep, typename _Period>
inline void
sleep_for(const chrono::duration<_Rep, _Period>& __rtime)
...
为什么 _GLIBCXX_USE_NANOSLEEP
未定义?
如何获取此示例编译?
Why is _GLIBCXX_USE_NANOSLEEP
not defined?
How do I get this example to compile?
更新2012年9月17日(jogojapan):今天我遇到了同样的问题,使用GCC 4.7。 1。我想知道是否有任何消息,如何避免它,除了通过定义 _GLIBCXX_USE_NANOSLEEP
。我尝试使用 -std = gnu11
,但无效。
Update 17 Sep 2012 (jogojapan): I ran into this same problem today, using GCC 4.7.1. I wonder if there is any news on how to avoid it, other than by defining _GLIBCXX_USE_NANOSLEEP
. I tried using -std=gnu11
, but to no avail.
还有一个旧的,未解决的错误报告适用于GCC 4.4: https://bugs.launchpad.net/ubuntu/+source/gcc -4.4 / + bug / 608145
There is also an old, unresolved bug report for GCC 4.4: https://bugs.launchpad.net/ubuntu/+source/gcc-4.4/+bug/608145
2012年10月19日更新(jogojapan ):这个问题现在由Jonathan Wakely解释并解决了,作为这个问题的一个问题:什么是_GLIBCXX_USE_NANOSLEEP所有?
这对于那些自己编译GCC而不是使用现成的软件包的人来说尤其相关。
Update 19 Oct 2012 (jogojapan): The issue has now been explained and resolved by Jonathan Wakely as an anwer to this question: What is _GLIBCXX_USE_NANOSLEEP all about? This is particularly relevant for anyone who builds GCC himself instead of using a ready-made package.
推荐答案
确认它在这里也不工作。 (最近的GCC 4.6快照)。
Confirmed that it doesn't work here as well. (Recent GCC 4.6 snapshot).
你可以做明显的,简单地定义它,然后才包括任何std :: headers。有点脏,但会工作,直到GCC修复它(除非这是预期的行为)。 #define不应该打破任何东西。可以在source或-D_GLIBCXX_USE_NANOSLEEP标记到GCC。
You could do the obvious and simply define it before you include any std:: headers. A bit dirty but will work until GCC fixes it (unless this is intended behavior). The #define shouldn't break anything anyways. Either in source or -D_GLIBCXX_USE_NANOSLEEP flag to GCC.
您可能想尝试使用-std = gnu ++ 0x而不是-std = c ++ 0x,因为gnu ++ 0x经常拉这样的东西。
You might want to try using -std=gnu++0x rather than -std=c++0x, since gnu++0x often pulls in stuff like this.
这篇关于std :: this_thread :: sleep_for()和GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!