Search code examples
c++consolesystemsystem-calls

Suppress console when calling "system" in C++


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.


Solution

  • It sounds like you're using windows.

    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.

    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.