Home > other >  Boost::process hide console on linux
Boost::process hide console on linux

Time:03-28

According to this question : Boost::process hide console on windows

How can I achieve the same but on Linux platform using boost::process ? (prevent console window creation on the newly spawn child process)

My use case is as followed:

I'm trying to call a cross-platform GUI app built from .NET 5.0 AvaloniaUI (C#). And I'm calling that from another GUI app built from gtkmm (c ). It does create black console along with GUI window.

This symptom doesn't occur when I tried calling the same mentioned Avalonia app with another cross-platform Avalonia app. It doesn't create any console, only the GUI window as intended. (Using System.Diagnostics.Process from .NET C#).

So maybe there is some detail behind that make these api behave differently? (boost::process / System.Diagnostics.Process)

Basically I'm looking for the api to do the same thing with System.Diagnostics.Process (C#) on c

CodePudding user response:

On linux a new console isn't created anyways. So, the only thing you might want to do is prevent the child process from sharing the parent's, like so:

#include <boost/process.hpp>

namespace bp = boost::process;
int main() {
    bp::child child(bp::search_path("xclock"), //
                    bp::std_in.null(),         //
                    bp::std_out.null(),        //
                    bp::std_err.null());

    child.wait();
}

If, on the other hand, you would like to create a terminal, you have to tell the OS which one you want and how to start that:

bp::child child(bp::search_path("gnome-terminal"),  //
                std::vector<std::string>{"-x", "/usr/bin/htop"}, //
                bp::std_in.null(),                  //
                bp::std_out.null(),                 //
                bp::std_err.null());

Or, e.g.

bp::child child(bp::search_path("gnome-terminal"),  //
                std::vector<std::string>{"-x", "/usr/bin/htop"}, //
                bp::std_in.null(),                  //
                bp::std_out.null(),                 //
                bp::std_err.null());

Note that the gnome-terminal launcher forks (so it "returns" immediately, leaving the terminal running with the htop inside), but xterm does not. So, on UNIX, like ever, you have lots of choices.

CodePudding user response:

Linux does not create new console, like the others suggested. (unless explicitly define to)

After more investigation, I found that, it's only a misunderstanding.

If you use some GUI lib (like GTKmm, in this case), and try to spawn new process, which is a GUI window also, there might be an afterimage-like effect if you don't do it asynchonously

(If that second GUI window is initialized with black background, like the one created with AvaloniaUI, you may confuse it as another console :p)

  • Related