Search code examples
c++boost

Set the working directory when starting a process with boost


I am looking for a way to specify the working directory when starting a process with boost::process::system or boost::process::child. In the docs https://www.boost.org/doc/libs/1_77_0/doc/html/boost_process/tutorial.html are some useful examples, but nothing on the subject of my interest.

The child constructor looks like this:

template<typename ...Args>
child::child(Args&&...args)

and I haven't found complete documentation covering what the Args might be, only some incomplete examples.


Solution

  • #include <boost/process/start_dir.hpp>
    
    namespace bp = boost::process;
    
    int result = bp::system("/usr/bin/g++", "main.cpp", bp::start_dir("/home/user"));
    
    bp::child c(bp::search_path("g++"), "main.cpp", bp::start_dir("/home/user"));
    c.wait();
    

    See boost::process::start_dir and the complete Reference.

    Args are program name, program args and other process properties from the Reference.