有没有一种方法可以记录通过boost::process::spawn创建的进程的输出



我知道有一种方法可以使用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才能做到这一点?

不,这是不可能的。它隐含在文档中:

此函数不允许异步操作,因为它不能等待过程结束。如果传递了对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;
}

最新更新