0

Need to set handle function for on_exit event.

Tried asignment like in boost tutorial.

#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 test -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_&&’

1 Answers1

0

The immediate cause of your compilation error is that you are missing the <boost/process/async.hpp> include.

However this will not be enough to make the code correct. As mentioned in https://www.boost.org/doc/libs/1_72_0/doc/html/boost/process/on_exit.html, you need to pass in your own io_context when using an on_exit handler.

Here is a code snippet that should work:

#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;
}
Xavier Leclercq
  • 313
  • 2
  • 8