0

需要为 on_exit 事件设置句柄函数。

像在 boost 教程中一样尝试分配。

#include <boost/process.hpp>
#include <iostream>
using namespace boost::process;
using err_code = boost::system::error_code;
int main() {
    child proc("ls -l", boost::process::on_exit = [](int exit, const err_code& ec_in){std::cout << "hah" << std::endl;});
    proc.wait();
    int ret = proc.exit_code();
    return 0;
}

g++ test.cpp -o 测试 -lboost_thread -lboost_system -pthread

test-proc.cpp: In function ‘int main()’:
test-proc.cpp:6:119: error: no match for ‘operator=’ (operand types are ‘const boost::process::detail::on_exit_’ and ‘main()::<lambda(int, const err_code&)>’)
     child proc("ls -l", boost::process::on_exit = [](int exit, const err_code& ec_in){std::cout << "hah" << std::endl;});
                                                                                                                       ^
In file included from /usr/include/boost/process/async.hpp:33:0,
                 from /usr/include/boost/process.hpp:23,
                 from test-proc.cpp:1:
/usr/include/boost/process/detail/on_exit.hpp:40:19: note: candidate: boost::process::detail::posix::on_exit_ boost::process::detail::on_exit_::operator=(const std::function<void(int, const std::error_code&)>&) const
     api::on_exit_ operator= (const std::function<void(int, const std::error_code&)> & f) const {return f;}
                   ^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:40:19: note:   no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘const std::function<void(int, const std::error_code&)>&’
/usr/include/boost/process/detail/on_exit.hpp:43:19: note: candidate: boost::process::detail::posix::on_exit_ boost::process::detail::on_exit_::operator=(std::future<int>&) const
     api::on_exit_ operator= (std::future<int> &f) const {return on_exit_from_future(f);}
                   ^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:43:19: note:   no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘std::future<int>&’
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: candidate: constexpr boost::process::detail::on_exit_& boost::process::detail::on_exit_::operator=(const boost::process::detail::on_exit_&)
 struct on_exit_
        ^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:38:8: note:   no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘const boost::process::detail::on_exit_&’
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: candidate: constexpr boost::process::detail::on_exit_& boost::process::detail::on_exit_::operator=(boost::process::detail::on_exit_&&)
/usr/include/boost/process/detail/on_exit.hpp:38:8: note:   no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘boost::process::detail::on_exit_&&’
4

1 回答 1

0

编译错误的直接原因是您缺少 <boost/process/async.hpp> 包含。

但是,这不足以使代码正确。如https://www.boost.org/doc/libs/1_72_0/doc/html/boost/process/on_exit.html中所述,您需要在使用 on_exit 处理程序时传入自己的 io_context。

这是一个应该可以工作的代码片段:

#include <boost/process/child.hpp>
#include <boost/process/async.hpp>
#include <iostream>

int main(int argc, char* argv[])
{
    boost::asio::io_context execution_context;
    auto on_exit_handler = [](int exit, const std::error_code& ec_in)
    {
        std::cout << "Child process has finished." << std::endl;
    };
    boost::process::child childProcess("HelloWorld", execution_context, boost::process::on_exit = on_exit_handler);
    execution_context.run();

    return 0;
}
于 2020-02-08T22:34:28.197 回答