Cygwin错误:系统未在此范围内声明?

摇滚乐

我目前正在使用Cygwin进行实验,当我遇到此问题时才从今天开始尝试。

这是我用于测试的MSVS 2010 C ++代码:

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
   cout << "Hello, world!" << endl;
   system("pause");
   return 0;
}

编译它并在Visual Studio本身上运行就可以了。但是在Cygwin bash上,插入以下命令后:

 $ g++ -Wall -g -o Test.exe main.cpp

当我运行时Test.exe,控制台几乎立即关闭,并且在Hello World行下显示如下内容:

sh:pause: command not found.

由于关闭非常快,因此很难看到。有人知道为什么吗?

不知道这是否相关,但是当

#include <cstdlib> 

被排除,cygwin bash上的错误将是:

$ g++ -Wall -g -o Test.exe main.cpp
main.cpp: In function `int main()':
main.cpp:10:18: error: `system' was not declared in this scope
system("pause");
                  ^

这是解决错误的正确方法吗?关于Cygwin的文档和教程很难找到,因此我也不完全确定是否#include <cstdlib>正确。

船长

如果您确实需要它在两种环境中都能工作,请致电system("cmd /C pause")而不是system("pause")这将强制Windows Shell启动,然后调用其暂停命令。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章