0

I would like to use Boost::Process::Child to create a process while also supplying an environment variable to that process. While it seems straightforward, I've also got the requirement of compiling with -Wall -Wextra -pedantic -Werror -Wl,--fatal-warnings (Basically, the slightest issue is treated as an error).

I'm following the Boost tutorial (https://www.boost.org/doc/libs/1_80_0/doc/html/boost_process/tutorial.html#boost_process.tutorial.env) and the answer in a duplicate question here: Create child process with custom environment using boost for something similar to this:

std::string command = "/usr/bin/something";
ipstream pipe_stream;

auto env = boost::this_process::environment();
env["SOMETHING"] = "VALUE";

boost::process::child childProc(command, env, std_out > pipe_stream);

This works until I add my compiler switches. Now I'm getting an error:

/usr/lib/boost/boost/process/env.hpp:309:13: error: implicitly-declared 'boost::process::basic_environment<char>& boost::process::basic_environment<char>::operator=(const boost::process::basic_environment<char>&)' is deprecated [-Werror=deprecated-copy]
  309 |         env = e;
      |         ~~~~^~~

And some additional notes:

/usr/lib/boost/boost/process/detail/posix/environment.hpp:177:30: note: because 'boost::process::basic_environment<char>' has user-provided 'boost::process::detail::posix::basic_environment_impl<Char>& boost::process::detail::posix::basic_environment_impl<Char>::operator=(const boost::process::detail::posix::basic_environment_impl<Char>&) [with Char = char]'
  177 |     basic_environment_impl & operator=(const basic_environment_impl& rhs)
      |                              ^~~~~~~~

/usr/lib/boost/boost/process/env.hpp:309:13: note: synthesized method 'boost::process::basic_environment<char>& boost::process::basic_environment<char>::operator=(const boost::process::basic_environment<char>&)' first required here
  309 |         env = e;
      |         ~~~~^~~

It seems the boost::process::child constructor uses a now-deprecated basic_environment copy constructor, but the documentation still shows this as the appropriate method.

How do I construct a boost::process::child with an environment now without this warning/error?

mitchellJ
  • 734
  • 3
  • 9
  • 32

2 Answers2

0

Like the commenter, I'm unable to reproduce this on my system.

So we really need to know more about what tools you use to compile, and what you're compiling (versions).

That said, I think the issue can be skirted by making the Boost include a system include, e.g. with -isystem /path/to/boost or add_includes(SYSTEM /path/to/boost) and similar when using CMake.

sehe
  • 374,641
  • 47
  • 450
  • 633
0

You shouldn't use the example from Boost tutorial. Try to use the following instead:

boost::process::environment env = boost::this_process::native_environment();
  • This is more of an aside and not an appropriate answer to the question. You don't explain why not to use examples from the boost tutorial, or precisely why to use the method you have listed instead. It also doesn't actually answer the original question that I had asked. You are marked as a new contributor; otherwise, I would downvote this answer. – mitchellJ Dec 12 '22 at 20:29
  • You have to copy environment and pass the copy (not the env of the parent process) to the child process. Just like in their example they do: bp::environment env_ = env;. If you simply call env = boost::this_process::environment() and then pass env (handle of the parent process) to the child process it can cause some undesired behavior later on. This is what I got when used it like this. So, I just create a copy using boost::this_process::native_environment(). Probably, compilation errors are here to avoid passing the handle of the parent process itself to the child process. – anto6ka Dec 27 '22 at 13:52