问题描述
我知道有一种方法可以使用 boost::process::child 描述的 here:
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 来这样做吗?
解决方法
不,那是不可能的。它隐含在 documentation 中:
这个函数不允许异步操作,因为它不能 等待过程结束。编译失败 传递了对 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;
}