当我尝试编译这个简单的程序时:
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();
}
在 Ubuntu 10.04(32 位)上使用 gcc 4.4.3 版:
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’
我查看了标题线程".
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:: 标头之前,您可以做显而易见的事情并简单地定义它.有点脏,但会一直工作到 GCC 修复它(除非这是预期的行为).#define 无论如何都不应该破坏任何东西.在源代码或 GCC 的 -D_GLIBCXX_USE_NANOSLEEP 标志中.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!