0

我知道有一种 boost::process::child 的方法,如下所述

boost::asio::boost::asio::io_service ios;

std::future<std::string> data;
child c("g++", "main.cpp", //set the input
        bp::std_in.close(),
        bp::std_out > bp::null, //so it can be written without anything
        bp::std_err > data,
        ios);


ios.run(); //this will actually block until the compiler is finished

auto err =  data.get();

这可以在调用 boost::process::spawn 时工作还是我必须使用 boost::process::child 来这样做?

4

1 回答 1

1

不,那是不可能的。它在文档中暗示:

此函数不允许异步操作,因为它不能等待进程结束。如果传递了对 boost::asio::io_context 的引用,它将无法编译。

也许你可以child::detach改用?

#include <boost/process.hpp>
#include <boost/asio.hpp>
#include <iostream>
namespace bp = boost::process;

int main()
{
    boost::asio::io_service ios;

    std::future<std::string> data;

    bp::child c("/usr/bin/g++", "main.cpp", // set the input
                bp::std_in.close(),
                bp::std_out > bp::null, // so it can be written without anything
                bp::std_err > data, ios);
    c.detach();

    ios.run(); // this will actually block until the compiler is finished

    auto err = data.get();
    std::cout << err;
}
于 2021-05-21T16:04:55.040 回答