调用“系统"时抑制控制台在 C++ 中

2022-01-11 00:00:00 console system c++ system-calls

我在 C++ 中使用 system 命令来调用一些外部程序,每当我使用它时,控制台窗口都会在命令完成后打开和关闭.

I'm using the system command in C++ to call some external program, and whenever I use it, a console window opens and closes after the command finishes.

如何避免打开控制台窗口?如果解决方案可以独立于平台,我会很高兴.我还想让我的程序等到命令完成.

How can I avoid the opening of a console window? I would be happy if the solution could be platform-independent. I would also like for my program to wait until the command is finished.

推荐答案

听起来你在用windows.

It sounds like you're using windows.

在 Linux(和一般的 *nix)上,我会分别将对 system 的调用替换为对 forkexec 的调用.在 Windows 上,我认为 Windows API 中有某种 spawn-a-new-process 函数――请查阅文档.

On Linux (and *nix in general), I'd replace the call to system with calls to fork and exec, respectively. On windows, I think there is some kind of spawn-a-new-process function in the Windows API—consult the documentation.

当您运行 shell 命令和/或外部程序时,您的程序很难独立于平台,因为它取决于具有您正在运行的命令和/或外部程序的平台.

When you're running shell commands and/or external programs, your program is hard to make platform-independent, as it will depend on the platform having the commands and/or external programs you're running.

相关文章