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 #include
I needed to use!
144
votes
If you use any mayor IDE(NetBeans,IntelliJ IDEA, Eclipse). type the name of any function, then Alt+Enter it will auto-import the library that has it.
– T04435
@T04435: In C libraries are not imported. The compiler does not need them. The linker might link them, but only after the compiler is done. In C the compiler needs a prototype of a function to to use a function. Prototypes typically come in header files (.h).
– alk
5 Answers
189
votes
The sleep man page says it is declared in <unistd.h>
.
Synopsis:
#include <unistd.h>
unsigned int sleep(unsigned int seconds);
77
votes
70
votes
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;
}
15
votes
9
votes
sleep(3)
is in unistd.h
, not stdlib.h
. Type man 3 sleep
on 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
.