C++错误:Sleep在此作用域中未声明

20

我在Ubuntu上使用codeBlocks和GCC 4.7中的boost 1.46 [ yield_k.hpp ],使用C++语言。

我遇到了以下编译时错误:

error : Sleep was not declared in this scope

代码:

#include <iostream>
using namespace std;
int main() { 
  cout << "nitrate";
  cout << flush;
  sleep(1000);
  cout << "firtilizers";
  return 0;
}

我该如何解决这个错误?我希望程序暂停一秒钟。


12
你需要写成#include <unistd.h>,带上尖括号。 - Kerrek SB
@KerrekSB 如果那些东西缺失了,他不应该在#include行上收到错误吗? - daramarak
我包含了 #include <unistd.h> 但是没有起作用,我还把 Sleep() 改成了 sleep()。 - Mahika
sleep(),小写字母,定义在<unistd.h>中,参见:http://pubs.opengroup.org/onlinepubs/7908799/xsh/sleep.html。现在,我的猜测是头文件中有些混淆了,所以我从头开始,看看哪些会中断编译... - Digital Da
在我的情况下,包括 #include <unistd.h> 在小写字母s的情况下可以工作。 - y_159
显示剩余7条评论
5个回答

29

Sleep是Windows函数。

对于Unix系统,可以考虑使用nanosleep(POSIX标准)或usleep(BSD系统;已弃用)。

下面是一个nanosleep示例:

void my_sleep(unsigned msec) {
    struct timespec req, rem;
    int err;
    req.tv_sec = msec / 1000;
    req.tv_nsec = (msec % 1000) * 1000000;
    while ((req.tv_sec != 0) || (req.tv_nsec != 0)) {
        if (nanosleep(&req, &rem) == 0)
            break;
        err = errno;
        // Interrupted; continue
        if (err == EINTR) {
            req.tv_sec = rem.tv_sec;
            req.tv_nsec = rem.tv_nsec;
        }
        // Unhandleable error (EFAULT (bad pointer), EINVAL (bad timeval in tv_nsec), or ENOSYS (function not supported))
        break;
    }
}
您需要使用<time.h><errno.h>,在C++中可用作<ctime><cerrno>usleep更简单(只需乘以1000,因此将其作为内联函数)。但是,无法保证睡眠会持续一定的时间,它已过时,您需要包含<unistd.h>并使用 extern "C" { }
第三个选择是使用selectstruct timeval,如在http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204中所示(这是wine如何模拟Sleep,而Sleep本身只是SleepEx的包装器)。 注意:小写字母's'的sleep,其声明位于<unistd.h>中,不是一个可接受的替代方案,因为它的粒度是秒,比Windows的Sleep(大写's')更粗,后者的粒度为毫秒。
关于您的第二个错误,___XXXcall是MSVC++特定的标记(如__dllXXX__naked__inline等)。如果您确实需要stdcall,请使用__attribute__((stdcall))或类似方法在gcc中模拟它。 注意:除非您的编译目标是Windows二进制文件,并且正在使用Win32 API,否则对于stdcall的使用或要求是一个不好的信号。

谢谢moshbear,我通过将'Sleep'更改为'sleep'来解决了问题,并且由于我需要使用stdcall,所以使用了您的解决方案__attribute__((stdcall))。谢谢。 - Mahika
1
“sleep” 命令的单位是秒,而不是毫秒;最好像 wine 中那样编写一个 select 循环(Winsock 也支持“select”),或者将 my_sleep 复制粘贴过来。在我看来,“nanosleep” 比“select”更优雅。别忘了添加“#ifndef _WINDOWS”或相应的内容。 - moshbear
1
@timothy:调用POSIX函数时不需要使用__attribute__((stdcall)) - Keith Thompson
那个winehq源代码,行号可能已经过时了,请尝试这样做: - pilkch

17

如何在 Linux 上使用 usleep 进行 C++ 编程:

将以下内容放入名为 s.cpp 的文件中:

#include <iostream>
#include <unistd.h>
using namespace std;
int main() { 
  cout << "nitrate";
  cout << flush;
  usleep(1000000);
  cout << "firtilizers";
  return 0;
}

编译并运行它:

el@defiant ~/foo4/40_usleep $ g++ -o s s.cpp
el@defiant ~/foo4/40_usleep $ ./s
nitratefirtilizers

它打印了“硝酸盐”,等待1秒钟,然后打印了“肥料”


4
#include <unistd.h>解决了我遇到的问题,谢谢! - seth10

3
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
    const long a=1000000;
    long j;
    cin >> j;
    usleep(a*j);
    puts("exit");
}

使用 usleep() 替代 sleep,别忘了包含 unistd.h(不是 cunistd


1
在我的情况下,写Sleep而不是sleep有所帮助 - 非常奇怪,但有效!

1
你在哪个操作系统环境下?是Windows还是Linux? - hungryWolf

1

使用std::this_thread::sleep_for()

#include <chrono>
#include <tread>


int main(int argc , char *argv[])
{       
    std::this_thread::sleep_for(std::chrono::seconds(2));
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接