20

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.

jbsu32
  • 1,036
  • 1
  • 11
  • 31
Mahika
  • 662
  • 2
  • 11
  • 21
  • 12
    You have to write `#include `, with the angled brackets. – Kerrek SB Jun 11 '12 at 07:53
  • @KerrekSB shouldn't he get an error on the #include line if those were missing? – daramarak Jun 11 '12 at 07:57
  • @daramarak, The only sure thing is "nothing worked". There could have been an error on both lines. Not exactly sure how boost plays into this either. It's just a mention, not a boost-related question. – chris Jun 11 '12 at 07:58
  • 1. Shouldn't `sleep()` be with a lowercase `s`? 2. I guess the `expected initialized before 'sleep'` probably means that you forgot a semicolon in one of the lines before `sleep()`... – Eitan T Jun 11 '12 at 08:06
  • I included #include dint work, and I also changed Sleep() to sleep() – Mahika Jun 11 '12 at 08:08
  • error definition: /home/mypc/Down/boost_1_48_0/boost/smart_ptr/detail/yield_k.hpp:88:18: error: ‘Sleep’ was not declared in this scope P.S. Boost 1.48 is in use – Mahika Jun 11 '12 at 08:10
  • @EitanT suggestion sounds plausible. But we can only guess without any code. – daramarak Jun 11 '12 at 08:22
  • sleep(), with a lower case, is defined in , see: http://pubs.opengroup.org/onlinepubs/7908799/xsh/sleep.html Now, my guess is that something is getting confused in the headers, so I start from scratch and see what interrupts compilation.. – Digital Da Jun 11 '12 at 08:46
  • Digital Da, I did try your suggestion , but still does not work. – Mahika Jun 11 '12 at 08:51
  • I get an error:expected initializer before 'sleep' extern "C" void __stdcall sleep( unsigned long ms ); – Mahika Jun 11 '12 at 08:51
  • In my case including `#include ` works with `s` in lowercase. – y_159 Dec 26 '20 at 14:59
  • I was getting this error even after adding `#include` . – y_159 Dec 26 '20 at 15:05

5 Answers5

29

Sleep is a Windows function.

For Unix, look into using nanosleep (POSIX) or usleep (BSD; deprecated).

A nanosleep example:

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;
    }
}

You will need <time.h> and <errno.h>, available in C++ as <ctime> and <cerrno>.

usleep is 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 to extern "C" { }-include <unistd.h>.

A third choice is to use select and struct timeval, as seen in http://source.winehq.org/git/wine.git/blob/HEAD:/dlls/ntdll/sync.c#l1204 (this is how wine emulates Sleep, which itself is just a wrapper for SleepEx).

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, ___XXXcall is 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 stdcall is A Bad Sign™.

moshbear
  • 3,282
  • 1
  • 19
  • 33
  • Thanks moshbear, I fixed it by changing 'Sleep' to 'sleep' and since I needed to use stdcall, is used your solution __attribute__((stdcall)). Thanks – Mahika Jun 11 '12 at 09:24
  • 1
    `sleep` does seconds, not milliseconds; you'd be better off writing a select loop as is done in wine (`select` is also supported by Winsock) or copy pasting the my_sleep. `nanosleep` is a bit more elegant than `select`, IMO. Don't forget the `#ifndef _WINDOWS` or equivalent. – moshbear Jun 11 '12 at 10:06
  • 1
    @timothy: You shouldn't need `__attribute__((stdcall))` to call POSIX functions. – Keith Thompson Jul 04 '13 at 06:22
  • That winehq source code, the line number might be out of date, try this: – pilkch Jun 06 '17 at 03:14
17

How to use usleep in a C++ program on linux:

Put this in a file called s.cpp

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

Compile it and run it:

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

It printed 'nitrate', waited 1 second, then printed 'firtilizers'

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
3
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
    const long a=1000000;
    long j;
    cin >> j;
    usleep(a*j);
    puts("exit");
}

use usleep() Insted of sleep and Don't forget to Include unistd.h (Not cunistd)

1

In my case it helped to write Sleep and NOT sleep - very strange, but worked!

alex
  • 11
  • 1
1

use std::this_thread::sleep_for()

#include <chrono>
#include <tread>


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