未定义的"睡眠"参考,但我确实包括<unistd.h>

Men*_*phy 12 c windows winapi sleep

嗯,这一定是个傻瓜.下面是C中不可能更简单的代码.它不能编译说"未定义的睡眠引用".但我想我包括我需要的所有系统头...

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

    return 1;
}
Run Code Online (Sandbox Code Playgroud)

waz*_*bit 36

在顶部试试这个:

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

此代码允许您使用相同名称下的两个不同功能,这些功能完全相同.它在不同平台下编译.

此外,括号(x)参数确保延迟是正确的,即使有人像这样调用它sleep(10+10)