Thread: Problem with function sleep()

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    14

    Question Problem with function sleep()

    Hello All,

    I have got a problem using delay function sleep() in Borland C++ 4.5 under Windows XP SP2.
    When I try to compile a code (example code below, for example) then I
    get an error message:

    :Call to undefined function sleep() in function main()

    How I could fix it?

    Code:
    /* sleep example */
    
    #include "dos.h"
    #include <stdio.h>
    
    int main(void)
    {
    int i;
    
    for (i=1; i<5; i++)
    {
    printf("Sleeping for &#37;d seconds\n", i);
    sleep(i);
    }
    return 0;
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    The sleep function is declared in unistd.h.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And the function Sleep() is defined in windows.h

    sleep() accepts seconds, whereas Sleep() accepts milliseconds both of which returns different values (search for Sleep() on MSDN, and then do a man sleep())

    HTH

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I don't believe Borland provides unistd.h.

    Since you're using Windows, you could always use the Sleep() function from the Windows API. You would need to include Windows.h and link with Kernel32.lib (not surprisingly).

    http://msdn2.microsoft.com/en-us/library/ms686298.aspx

    The following program uses the Win32 function Sleep() to pause after listing each command line argument one after the other:

    Code:
    #include <stdio.h>
    #include <Windows.h>
    
    int main(int argc, char **argv)
    {
    	int i;
    	
    	for(i=0;i<argc;i++)
    	{
    		printf("argv[&#37;d] = %s\n",i,argv[i]);
    		Sleep(1000);
    	}
    	
    	return 0;
    }
    Last edited by MacGyver; 04-05-2007 at 01:37 AM. Reason: Spelling

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by MacGyver View Post
    I don't believe Borland provides unistd.h.

    Since you're using Windows, you could always use the Sleep() function from the Windows API. You would need to include Windows.h and link with Kernel32.lib (not surprisingly).

    http://msdn2.microsoft.com/en-us/library/ms686298.aspx

    The following program uses the Win32 function Sleep() to pause after listing each command line argument one after the other:

    Code:
    #include <stdio.h>
    #include <Windows.h>
    
    int main(int argc, char **argv)
    {
    	int i;
    	
    	for(i=0;i<argc;i++)
    	{
    		printf("argv[%d] = %s\n",i,argv[i]);
    		Sleep(1000);
    	}
    	
    	return 0;
    }
    Unfortunately, it does not. I've got the same error message using your code as before - a nasty thing but what could be a reason?!

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by zacs7 View Post
    And the function Sleep() is defined in windows.h

    sleep() accepts seconds, whereas Sleep() accepts milliseconds both of which returns different values (search for Sleep() on MSDN, and then do a man sleep())

    HTH
    I have tried but it does not work either. Thanks anyway..

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well it seems from your first post that you're using a DOS compiler on a windows machine.

    Maybe try the delay() function (parameters unknown - read the manual).

    Or you could try getting one of the many excellent true win32 compilers and save DOS for the archaeologists.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by Salem View Post
    Well it seems from your first post that you're using a DOS compiler on a windows machine.

    Maybe try the delay() function (parameters unknown - read the manual).

    Or you could try getting one of the many excellent true win32 compilers and save DOS for the archaeologists.
    ...not really, it's 32 bit windows compiler i believe (actually there are both 16bit and 32 bit compilers in Borland C++ 4.51) and function sleep() is described in manual, but does not work for some reason.

    i've tried - there is the same problem using delay() function as with sleep()
    Last edited by vicst197; 04-05-2007 at 05:37 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe try being less vague than "doesn't work" ?

    Did you include windows.h ?
    Did you set the project to create windows (or console programs)?
    Did you...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > And the function Sleep() is defined in windows.h
    Capitals are important.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by Salem View Post
    > And the function Sleep() is defined in windows.h
    Capitals are important.
    Quote Originally Posted by Salem View Post
    Maybe try being less vague than "doesn't work" ?

    Did you include windows.h ?
    Did you set the project to create windows (or console programs)?
    Did you...
    When i compile code:
    Code:
    /* sleep example */
    
    #include <stdio.h>
    #include <windows.h>
    
    int main(void)
    {
       int i;
    
       for (i=1; i<5; i++)
       {
          printf("Sleeping for &#37;d seconds\n", i);
          sleep(i);
       }
       return 0;
    }
    it brings after:
    Linking sample.exe:
    Linker Warning: No module definition file specified: using defaults
    Linker Error: Undefined symbol _sleep in module SAMPLE.C
    Last edited by vicst197; 04-05-2007 at 06:05 AM.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Using Sleep() instead of sleep() brings after compiling:

    Compiling SAMPLE.C:
    Warning SAMPLE.C 13: Call to function 'Sleep' with no prototype in function main
    Linking sample.exe:
    Linker Warning: No module definition file specified: using defaults
    Linker Error: Undefined symbol _Sleep in module SAMPLE.C

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Borland (on your old compiler) doesn't use sleep. They use delay. Sleep, if you have it, as said, is in windows.h.


    Quzah.
    Last edited by quzah; 04-05-2007 at 07:04 AM.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    14
    Quote Originally Posted by quzah View Post
    Borland doesn't use sleep. They use delay. Sleep, if you have it, as said, is in windows.h.


    Quzah.
    Not at all, sleep() is described in Help of Borland C++ 4.51 and my attempt to use delay(i) instead of sleep(i)
    brings similar error message:

    Linking sample.exe:
    Linker Warning: No module definition file specified: using defaults
    Linker Error: Undefined symbol _delay in module SAMPLE.C
    Last edited by vicst197; 04-05-2007 at 07:08 AM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, you win some, you lose some. So read your docs and figure out what you're doing wrong.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. why do we require sleep?
    By jinx in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 07-14-2004, 08:21 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM