【问题标题】:Command system("pause") not found on Linux在 Linux 上找不到命令系统(“暂停”)
【发布时间】:2020-01-29 06:46:45
【问题描述】:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>

using namespace std;

int main() {
    string line;
    cout << "HW\n";
    getline(cin,line);
    cout << "Your line is - " << line << "\n";
    system("pause");
    return 0;
} 

我想自己对factorio无头服务器做gui,所以我需要执行一些bash脚本。我想我需要函数 system() 吗?

我认为我的 lib 路径有问题。请不要怪罪于错误安装的 vcpkg。路径是:

/opt/factorio/bin/x64/vcpkg/installed
/usr/include/c++/9/x86_64-redhat-linux
/usr/include/linux
/usr/include/c++/9/tr1

Command system() not found 表示 Visual Studio。

【问题讨论】:

  • 请看我的回答,如果对您有帮助,请接受(投票下方的绿色勾号)并投票。如果对您没有帮助,请写评论。
  • system("pause"); 是特定于 Windows 的 hack。我的建议是,无论谁教你使用它,都应该严厉地交谈。

标签: c++ linux fedora vcpkg


【解决方案1】:

system("pause"); 只能在 Windows 上使用。它运行 Windows 命令行“暂停”程序并等待它终止,然后再继续执行您的程序。这就是为什么在代码中使用它是一种不好的做法,无论您是在 Windows 还是 Linux 上运行代码。

这里有一个更好的方法可以达到同样的效果:

#include <iostream>
using namespace std;

int main() {
   do {
     cout << '\n' << "Press the Enter key to continue.";
   } while (cin.get() != '\n');

   return 0;
}

代替:

#include <iostream>
using namespace std;

int main() {
   system("pause");

   return 0;
}

【讨论】:

  • 是的,但是我也想执行 bash 脚本。我尝试使用系统来测试 exec bash。因此,如果它在 linux 上不存在,那么启动 .sh 或制作“cat”“ls”“cd”“echo”的方法是什么?
  • 如果您有一个要执行的 bash 脚本,那么这将有助于 ./script.sh。但我真的不明白你在这里问什么?
  • 对不起。我英语不好。在 C++ 程序中执行 bash 脚本。因为我想要 gui。
  • 我认为你把事情搞砸了。不要试图在你的 C++ 程序中执行 bash 脚本,因为那不会像你想的那样给出 GUI。在 Linux 上,您的 GUI 就是终端。
  • 其实没有。我没有否决您的回答。我只是在等待更好的答案或其他东西。我仍在寻找执行 bash 脚本的方法,我认为这很简单。现在我知道命令系统在 Windows 中很有用。您替换了“暂停”,但没有将命令放到终端。我试图支持你的答案,因为它实际上是我的代码行的智能替换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-30
  • 2020-11-19
  • 2019-05-17
  • 2017-12-20
  • 2012-06-29
  • 1970-01-01
  • 2013-02-22
相关资源
最近更新 更多