15

Well, this must be a silly one. Here below is a can-not-be-simpler code in C. It can not compile saying "undefined reference to sleep". But I think I include all system header I need...

#include <stdio.h>
#include <unistd.h>
int main()
{
    printf("Test starts.\n");
    sleep(1);
    printf("Test ends.\n");

    return 1;
}
3
  • 3
    "Undefined reference" sounds like it isn't linking to something it needs.
    – Kevin
    Commented Jan 6, 2012 at 20:52
  • 1
    OS/Compiler? Is there maybe more code than this with a typo? This compiles and links with gcc 4.6.1.
    – JoeFish
    Commented Jan 6, 2012 at 21:00
  • @MengfeiMurphy, What compiler are you using? What command line are you typing to compile it? Tell us everything you can. Commented Jan 6, 2012 at 21:13

3 Answers 3

45

Try this at the top:

#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * (x))
#endif

This code will allow you to use two different functions under the same name, that do quite the same thing. And it compiles under different platforms.

Also, parenthesizing the (x) argument ensures that the delay is right even if someone calls it like this sleep(10+10)

4
  • Wonderful! You are damn right! I am using win7 + WinGW. This is the first time I meet with portable issues in practice. Have you got any other similar examples? Commented Jan 6, 2012 at 22:16
  • Actually it doesn't come to my mind but you can do a lot of things in this way. It's clean and simple.
    – kwadr4tic
    Commented Jan 7, 2012 at 8:46
  • this does in fact work, besides my issues with the flags in codeblocks, i can now run code that i made on a linux system in windows thanks to this header
    – user5849816
    Commented Jul 15, 2017 at 21:27
  • how buggy is this windows crap? having to define a macro to make this work when the header defined it? nice trick (even if I knew about the Sleep millis method) I have edited just in case someone passes 1+3 to the macro Commented May 1, 2018 at 21:14
2

If you are running under the Windows operating system, include windows.h header file to your program and change sleep() to Sleep().

The problem would get disappeared.

I hope this helps.

1
  • 3
    Windows' Sleep() function is in milliseconds, while *Nix's sleep() is in seconds. The *NIX equivalent of Sleep() is usleep(). See Wazabit's answer for the correct way to make code that uses sleep() portable. Commented Nov 17, 2017 at 13:29
0

i was having the same problem and looking at this code when i realized the 's' in sleep is capital .i.e. Sleep(10) but we both wrote sleep(10)

2
  • Neither sleep() nor Sleep() are part of the C standard. sleep() is part of the UNIX standard and Sleep() is provided in the windows.h file but they don't do the same thing. It's important to know what files you're linking and read the documentation for the functions they define. This is more than a typo situation. Commented Oct 20, 2022 at 20:24
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Oct 23, 2022 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.