How to call sleep()?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: How to call sleep()?

  1. #1
    Join Date
    Dec 2012
    Posts
    13

    How to call sleep()?

    I have two problems with the code below:

    1. I cannot find a header file to #include that has the sleep function prototype.

    2. When I add my own sleep function prototype, I get an unresolved external reference error (for _sleep, not sleep).

    What must I #include to get the sleep function prototype?

    What lib must I include in the linker configuration to resolve the external reference?

    (I suspect that if I #include the correct header file, the second question might become moot.)

    -----

    The "man page" at http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx says the header file is <WinBase.h>.

    But #include'g only <WinBase.h> results in compilation errors.

    A response marked "answer" at http://social.msdn.microsoft.com/For...1-75084ad53f7d says <windows.h> [sic].

    #Include'g only <Windows.h> does eliminate the compilation errors.

    But apparently that does not bring in the sleep function prototype. Neither does also subsequently #include'g <WinBase.h>.

    (Which seems to be #include'd by <Windows.h> anyway.)

    -----

    But even with my own function prototype shown below, I get an unresolved external reference for _sleep.

    Note: not for sleep per se. Is that a symptom of my problem: my sleep reference is changed to _sleep? If so, how can avoid that?

    According to "man page" (see link above), the external should be resolve in kernel32.lib.

    And kernel32.lib does appear in the "Additional Dependencies" list under Configuration Properties \ Linker \ Input.

    PS: Since I am not using C++ features, I tried setting "Compile as C" under Configuration Properties \ C/C++ \ Advanced, to no avail.

    -----

    My code....

    #include "stdafx.h"
    #include <stdlib.h>
    #include <time.h>
    #include <Windows.h>
    void sleep(DWORD msec); // added later

    int _tmain(int argc, char* argv[])
    {
    time_t st, et;
    st = time(NULL);
    sleep(2000);
    et = time(NULL);
    printf("%ld\n%ld\n%ld\n", st, et, et-st);
    printf("press Enter to terminate");
    getchar();
    return 0;
    }

  2. #2
    Join Date
    Jul 2002
    Posts
    2,512

    Re: How to call sleep()?

    Possibly you mean Sleep function. Required header is Windows.h.
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

  3. #3
    Join Date
    Apr 1999
    Posts
    27,426

    Re: How to call sleep()?

    Quote Originally Posted by joeu2004 View Post
    I have two problems with the code below:

    1. I cannot find a header file to #include that has the sleep function prototype.
    C++ is case-sensitive. The function is Sleep, not sleep.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Dec 2012
    Posts
    13

    Re: How to call sleep()?

    Quote Originally Posted by Paul McKenzie View Post
    C++ is case-sensitive. The function is Sleep, not sleep.
    OMG, I forgot! Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center