C语言 C 中函数'sleep' 的正确#include 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14818084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the proper #include for the function 'sleep' in C?
提问by trludt
I am using the Big Nerd Ranch book Objective-C Programming, and it starts out by having us write in C in the first few chapters. In one of my programs it has me create, I use the sleep function. In the book it told me to put #include <stdlib.h>under the #include <stdio.h>part. This is supposed to get rid of the warning that says "Implicit declaration of function 'sleep' is invalid in C99". But for some reason after I put #include <stdlib.h>, the warning does not go away.. This problem does not stop the program from running fine, but I was just curious on which #includeI needed to use!
我正在使用 Big Nerd Ranch 的书《Objective-C Programming》,它首先让我们在前几章中用 C 编写。在我创建的其中一个程序中,我使用了睡眠功能。在书中它告诉我把零件放在#include <stdlib.h>下面#include <stdio.h>。这应该消除“C99 中函数‘睡眠’的隐式声明无效”的警告。但是由于某种原因,在我放置之后#include <stdlib.h>,警告并没有消失..这个问题并没有阻止程序正常运行,但我只是好奇#include我需要使用哪个!
回答by simonc
回答by md5
sleepis a non-standard function.
sleep是一个非标准函数。
- On UNIX, you shall include
<unistd.h>. - On MS-Windows,
Sleepis rather from<windows.h>.
- 在 UNIX 上,您应包括
<unistd.h>. - 在 MS-Windows 上,
Sleep则来自<windows.h>.
In every case, check the documentation.
在每种情况下,请检查文档。
回答by Romain VIOLLETTE
this is what I use for a cross-platform code:
这是我用于跨平台代码的内容:
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
int main()
{
pollingDelay = 100
//do stuff
//sleep:
#ifdef _WIN32
Sleep(pollingDelay);
#else
usleep(pollingDelay*1000); /* sleep for 100 milliSeconds */
#endif
//do stuff again
return 0;
}
回答by alk
For sleep()it should be
因为sleep()它应该是
#include <unistd.h>
回答by Carl Norum
sleep(3)is in unistd.h, not stdlib.h. Type man 3 sleepon your command line to confirm for your machine, but I presume you're on a Mac since you're learning Objective-C, and on a Mac, you need unistd.h.
sleep(3)在unistd.h,不是stdlib.h。键入man 3 sleep您的命令行,以确认您的机器上,但我相信你是一个Mac上,因为你学习Objective-C,并在Mac上,你需要unistd.h。

