You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Boost的process::child创建自定义环境子进程示例请求

Using Boost.Process child with Custom Environment (Full Example)

I totally get what you mean—Boost.Process's system() function is handy but lacks flexibility for things like capturing output or fine-grained process control. Here's a complete, working example using process::child that sets a custom environment, captures the child's stdout, waits for it to finish, and checks the exit code.

First, let's look at the full code, then break down the key parts:

#include <boost/process.hpp>
#include <boost/process/environment.hpp>
#include <iostream>
#include <string>
#include <vector>

namespace bp = boost::process;

int main() {
    // Step 1: Create a custom environment
    bp::environment env = bp::this_process::environment(); // Start with current env
    env["CUSTOM_VAR"] = "HelloFromParent"; // Add/override variables
    env["EXISTING_VAR"] = "ModifiedValue"; // Example: change an existing variable

    // Step 2: Set up pipes to capture child's stdout
    bp::ipstream child_stdout; // Input stream to read from child's stdout

    // Step 3: Launch the child process with custom env and pipe
    bp::child proc(
        "/path/to/your/executable", // Replace with your actual executable path
        bp::std_out > child_stdout, // Redirect stdout to our pipe
        env // Pass the custom environment
    );

    // Step 4: Read output from the child
    std::string line;
    std::cout << "Child process output:\n";
    while (std::getline(child_stdout, line)) {
        std::cout << line << "\n";
    }

    // Step 5: Wait for the process to complete and get exit code
    proc.wait();
    int exit_code = proc.exit_code();
    std::cout << "\nChild process exited with code: " << exit_code << "\n";

    return 0;
}

Key Breakdown:

  • Custom Environment Setup: We start by copying the parent's current environment with bp::this_process::environment(), then modify or add variables as needed. This is better than building an environment from scratch unless you want a completely clean slate.
  • Pipe for Output Capture: Using bp::ipstream and bp::std_out > child_stdout lets us capture the child's standard output directly into our program. You can do similar for stderr with bp::std_err.
  • Launching the Child: The bp::child constructor takes the executable path, redirection options, and the environment. For command-line arguments, you can pass them as additional parameters (e.g., "/bin/echo", "Hello", "World").
  • Waiting and Exit Code: proc.wait() blocks until the child finishes, and proc.exit_code() gives you the exit status to handle success/failure.

Platform Notes:

  • On Windows, executable paths might use backslashes, but Boost.Process handles both forward and backslashes. Alternatively, use bp::search_path("executable_name") to find the executable in PATH.
  • If you want a completely empty environment, initialize env as bp::environment() instead of copying the parent's.

Example with Arguments:

If you need to pass command-line arguments to the child, just add them to the child constructor:

bp::child proc(
    "/bin/echo",
    "My custom var is:", bp::environment::get("CUSTOM_VAR"), // Pass env var as arg
    bp::std_out > child_stdout,
    env
);

This should cover most common use cases where system() falls short—capturing output, waiting for the process, and using a custom environment. Let me know if you need help adapting this to your specific scenario!

内容的提问来源于stack exchange,提问作者zack evein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:15:35