简体   繁体   中英

Why won't _sleep(1000) compile in Xcode?

I have a program that is supposed to count down from 10 to 0, and every time it counts down it's supposed to wait one second then flush the output using cin.flush(). The professor demonstrated this in class and it worked perfectly, however, when I get home Xcode gives me an error saying that _sleep(1000) is the use of an undeclared identifier '_sleep' -- which is not supposed to be the case as I imported the special commands and it's only supposed to use _sleep in windows compilers.

In short, this needs to compile in both windows and mac compilers, which I am supposed to do by these compiler definitions. But for some reason Xcode keeps trying to tell me it's wrong.

#ifdef GPP
#include <unistd.h>
#else
#include <cstdlib>
#endif


int main()
{
    for (int timer = 10; timer >= 0; timer--)
    {
        cout << timer;
        cout.flush();

        //compiler functions
        #ifdef GPP
        Sleep(1); //One second to sleep on GPP compilers
        #else
        _sleep(1000);//On windows 1000ms sleep time
        #endif

        cout << '\r';
    }
}

Sleep and variants are not portable , they are OS specific. that's why we use the standard:

std::this_thread::sleep_for (std::chrono::milliseconds(your time here));

have you defined GPP anywhere? if not then the code will use _sleep(1000) which is windows specific and won't work on mac. it should work if, for example, compiled like this:

g++ -DGPP

But you still need to change Sleep to sleep, as there's no Sleep function either.

#ifdef GPP                                                                      
#include <unistd.h>                                                             
#else                                                                           
#include <cstdlib>                                                              
#endif                                                                          
#include <iostream>                                                             

using namespace std;                                                            

int main()                                                                      
{                                                                               
    for (int timer = 10; timer >= 0; timer--)                                   
    {                                                                           
        cout << timer;                                                          
        cout.flush();                                                           

        //compiler functions                                                    
#ifdef GPP                                                                      
        sleep(1); //One second to sleep on GPP compilers                        
#else                                                                           
        _sleep(1000);//On windows 1000ms sleep time                             
#endif                                                                          

        cout << '\r';                                                           
    }                                                                           
}  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM