Boost::process 在 Linux 上隐藏控制台

发布于 2025-01-17 12:19:59 字数 650 浏览 7 评论 0原文

根据这个问题: Boost::process hide console on windows

我怎样才能实现相同,但在 Linux 平台上使用 boost::process ? (防止在新生成的子进程上创建控制台窗口)

我的用例如下:

我尝试调用从 .NET 5.0 AvaloniaUI (C#) 构建的跨平台 GUI 应用程序。我从另一个用 gtkmm (c++) 构建的 GUI 应用程序调用它。 它确实会创建黑色控制台以及 GUI 窗口。

当我尝试使用另一个跨平台 Avalonia 应用程序调用上述相同的 Avalonia 应用程序时,不会出现此症状。它不会创建任何控制台,只会创建预期的 GUI 窗口。 (使用 .NET C# 中的 System.Diagnostics.Process)。

那么也许背后有一些细节使得这些 api 的行为有所不同? (boost::process / System.Diagnostics.Process)

基本上我正在寻找 api 在 c++ 上与 System.Diagnostics.Process (C#) 执行相同的操作

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++

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间☆小暴躁 2025-01-24 12:20:00

在Linux上,无论如何都不会创建新的控制台。因此,您唯一想做的就是防止孩子的过程分享父母的过程:另一方面

#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();
}

,如果您会喜欢才能创建终端,则必须告诉OS您想要哪个以及如何开始:

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());

或,例如

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());

请注意,gnome-terminal启动器叉(因此,它立即返回”,使终端运行htop内部),但是xterm没有。因此,在Unix上,就像以往一样,您有很多选择。

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.

怀念你的温柔 2025-01-24 12:20:00

Linux不会像其他建议一样创建新的控制台。(除非明确定义)

在进行了更多调查后,我发现这只是一种误解。

如果您使用一些gui lib(在这种情况下为gtkmm),然后尝试产生新的过程,这也是一个GUI窗口,如果您不这样做异步

(如果第二个GUI窗口是用 black 背景初始化的,就像使用avaloniaui创建的窗口一样,您可能会将其混淆为另一个控制台:p)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文