I am using C++ in Ubuntu with codeBlocks, boost 1.46 in GCC 4.7 [ yield_k.hpp ]
I get this compile time error:
error : Sleep was not declared in this scope
Code:
#include <iostream>
using namespace std;
int main() {
cout << "nitrate";
cout << flush;
sleep(1000);
cout << "firtilizers";
return 0;
}
How do I resolve this error? I want the program to hang for 1 second.
Sleepis a Windows function.For Unix, look into using
nanosleep(POSIX) orusleep(BSD; deprecated).A
nanosleepexample:You will need
<time.h>and<errno.h>, available in C++ as<ctime>and<cerrno>.usleepis simpler to use (just multiply by 1000, so make it an inline function). However, it’s impossible to guarantee that that sleeping will occur for a given amount of time, it’s deprecated, and you need toextern "C" { }-include<unistd.h>.A third choice is to use
selectandstruct timeval, as seen in http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204 (this is how wine emulatesSleep, which itself is just a wrapper forSleepEx).Note:
sleep(lowercase ‘s’), whose declaration is in<unistd.h>, is not an acceptable substitute, since its granularity is seconds, coarser than that of Windows’Sleep(uppercase ‘s’), which has a granularity of milliseconds.Regarding your second error,
___XXXcallis a MSVC++-specific token (as are__dllXXX,__naked,__inline, etc.). If you really need stdcall, use__attribute__((stdcall))or similar to emulate it in gcc.Note: unless your compile target is a Windows binary and you’re using Win32 APIs, use of or a requirement for
stdcallis A Bad Sign™.