为什么 _sleep(1000) 不能在 Xcode 中编译?
Why won't _sleep(1000) compile in Xcode?
我有一个程序应该从10倒计时到0,每次倒计时都应该等待一秒钟,然后使用cin.flush()刷新输出。教授在课堂上演示了这一点,但它运行得很好,当我回到家时,Xcode给了我一个错误,说_sleep(1000)是使用了一个未声明的标识符"_sleep"——在我导入特殊命令时,情况并非如此,它只应该在windows编译器中使用_sleep。
简而言之,这需要在windows和mac编译器中进行编译,而我应该通过这些编译器定义来完成。但由于某种原因,Xcode一直试图告诉我这是错误的。
#ifdef GPP
#include <unistd.h>
#else
#include <cstdlib>
#endif
int main()
{
for (int timer = 10; timer >= 0; timer--)
{
cout << timer;
cout.flush();
//compiler functions
#ifdef GPP
Sleep(1); //One second to sleep on GPP compilers
#else
_sleep(1000);//On windows 1000ms sleep time
#endif
cout << 'r';
}
}
睡眠和变体是不可移植的,它们是特定于操作系统的。这就是我们使用标准的原因
std::this_thread::sleep_for (std::chrono::milliseconds(your time here));
您在任何地方定义了GPP吗?如果没有,那么代码将使用sleep(1000),它是windows特有的,不会在mac上工作。例如,如果像这样编译,它应该可以工作:
g++ -DGPP
但你仍然需要将"睡眠"改为"睡眠",因为也没有"睡眠"功能。
#ifdef GPP
#include <unistd.h>
#else
#include <cstdlib>
#endif
#include <iostream>
using namespace std;
int main()
{
for (int timer = 10; timer >= 0; timer--)
{
cout << timer;
cout.flush();
//compiler functions
#ifdef GPP
sleep(1); //One second to sleep on GPP compilers
#else
_sleep(1000);//On windows 1000ms sleep time
#endif
cout << 'r';
}
}
相关文章:
- SFML在使用Xcode 11和macOS Catalina(zsh)编译后崩溃
- 我不能用C++编译Xcode库
- 将XCode升级到verison 11.1后,C++编译失败
- 某些C++标头在放入 Xcode 前缀/预编译标头时会中断构建
- C 11模板代码 - 无法在XCode中编译
- 尝试在终端(MacOSX)中编译两个.cpp文件(来自XCode)时"Duplicate symbol"
- XCode预编译标头中的C 语言的预处理器定义了什么?
- Xcode - 如何使用"-fexhaustive-register-search"进行编译 - 链接器崩溃需要
- C++静态库未在 Mac 上的 Xcode 中编译
- 如果标头位于预编译标头 xcode 中,则找不到默认模板参数
- 编译 c++ 代码时 xcode 上的线程
- 由于 LLVM 错误,Xcode 无法编译
- 在 xcode 中编译和链接 OpenCV 3.0.0 时出现问题
- 我可以在 Android Studio 中使用用 Xcode 编译的 C++ 静态库吗?
- 我可以在 BlackBerry 10 中使用 Xcode 编译的 armv7/armv7s .so 库吗?
- 从命令行编译时"error: expected expression",但不从 Xcode 编译时
- Xcode 编译C++代码,但 Eclipse 不会
- 使用XCode编译时期望的类名与类名相同的c++
- Xcode:编译而不运行
- 通过xcode编译我的代码在OSX中使用mongodb c驱动程序