0

I made a simple snippet of code to record what the user input:

#include <stdio.h>
#include <unistd.h>

int main() {
  char letter;
  printf("Enter in a character:");
  scanf("%c", &letter);
  sleep(2);
  printf("Letter was: %c", letter);
  return 0;
}

In this code, it's supposed to sleep for 2 seconds on line 8.

Unfortunately, sleep is an undefined reference, even though I have the correct library.

Is there any way to fix this problem in Windows Vista?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
slippery
  • 447
  • 3
  • 5
  • 15
  • You are only showing that you are including the correct **header**, not necessarily linking against the right **library**. What is the command line you are using to compile and link? – Jonathon Reinhart Aug 03 '12 at 01:52
  • 1
    Well, a simple `gcc a.c` is working perfectly with your code in my Ubuntu. – Deqing Aug 03 '12 at 01:54
  • Do you mean Sleep() or sleep()? They aren't the same. What OS are you running on? – Jim Balter Aug 03 '12 at 03:05
  • It works by using 'gcc', which OS and compiler do you use? – aasa Aug 03 '12 at 03:08
  • Since the code uses `` the OS should be either a fully POSIX compliant one, or a virtual environment like MinGW or Cygwin. All those should should, by default, link with a standard library that should contain the `sleep` function. – Some programmer dude Aug 03 '12 at 05:52
  • @zeldarulez, to help us help you, you need to edit your question to include the complete error message. And not only the one regarding `sleep` but the complete output without modifications. The only "error" I can see in your code is that it's missing a `return` statement in the `main` function. – Some programmer dude Aug 03 '12 at 05:55
  • @Joachim: `return` statement shouldn't prevent compilation and linking. @OP post the complete compilation output. – askmish Aug 03 '12 at 09:14
  • @JoachimPileborg You mean '', not ''. And MinGW, which the OP is using, does not provide a POSIX environment. It provides but does not provide the supporting library; thus the OP's problem. – Jim Balter Aug 04 '12 at 21:47
  • @JimBalter Ah yes that's what I meant. – Some programmer dude Aug 05 '12 at 06:44

4 Answers4

5

sleep() is a POSIX function, but you are using MinGW, which does not provide POSIX support. To quote http://lists-archives.com/mingw-users/01723-including-library-for-unistd-h-header.html

Remember that mingw does not provide any more Unix APIs than what the Microsoft C runtime provides. (Well, actually it does, but only a bit more, no "difficult" things.) The Microsoft C runtime provides Unixish functions like open(), read(), close(), but not functions like getuid(), getegid(), getppid().

Nor sleep(). If you want a POSIX environment, you need Cygwin or some other POSIX support on Windows; see http://en.wikipedia.org/wiki/POSIX#POSIX_for_Windows

If you only need what MinGW gives you + sleep(), you can use the Windows Sleep() function (note that it is capitalized), which takes an argument in milliseconds and requires that you include Windows.h (rather than unistd.h); see sleep function in Windows, using C

Community
  • 1
  • 1
Jim Balter
  • 16,163
  • 3
  • 43
  • 66
2

If a program has to sleep for 2 seconds you have enter 2000 inside sleep function..

Sleep(unsigned int) :- unsigned int is the number of milliseconds ( 1 second= 1000milliseconds)

Please give "s" as captial letter in Sleep(5000);

and include the header file Windows.h

hackwithharsha
  • 901
  • 1
  • 16
  • 39
1

If it is an undefined reference, that means the linker is not linking the library containing sleep to your code.

You may have included unistd.h (which gives you appropriate function prototypes), but you must also link to the library containing the implementation of sleep.

Show how you are compiling and linking the program.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

Include the following function at the starting of your code, whenever you want to for the execution to pause without putting extra burden on CPU (in case of for loop)

void sleep(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}
user2876907
  • 135
  • 1
  • 5
  • 2
    Sorry, but this is not a proper answer, you should be trying to get sleep to work, instead of doing a workaround. This solution could be dangerous... besides, a loop, even without extra code, will put extra burden on CPU – tf.alves Sep 19 '14 at 10:35