title: How to capture the exit_code and stderr of the command that is run in C++?
body: <p>I'm writing a c++ program that executes and outputs (in real-time) a shell script, makefile or just another program. However I would like to have my program return differently when there are errors or no error.</p>
  
  <pre><code>#include "execxi.h"
  
  
  
  using namespace std;
  
  
  int execXI::run(string command)
  {
  
      FILE *in;
      char buff[512];
      // is this the check for command execution exited with not 0?
      if(!(in = popen(command.c_str(), "r"))){
              // I want to return the exit code and error message too if any
          return 1;
      }
      // this part echoes the output of the command that's executed
      while(fgets(buff, sizeof(buff), in)!=NULL){
          cout &lt;&lt; buff;
      }
      pclose(in);
      return 0;
  
  
  
  }
  </code></pre>
  
  <p>is what I have so far. </p>
  
  <p>Let's say this script ran <code>make</code> to build a program and it gave an error like so</p>
  
  <pre><code>on_target_webkit_version out/Release/obj/gen/webkit_version.h
  Traceback (most recent call last):
    File "../build/webkit_version.py", line 107, in &lt;module&gt;
      sys.exit(main())
    File "../build/webkit_version.py", line 103, in main
      return EmitVersionHeader(*sys.argv[1:])
    File "../build/webkit_version.py", line 86, in EmitVersionHeader
      webkit_revision = GetWebKitRevision(webkit_dir, version_file)
    File "../build/webkit_version.py", line 60, in GetWebKitRevision
      version_info = lastchange.FetchVersionInfo(
  AttributeError: 'module' object has no attribute 'FetchVersionInfo'
  make: *** [out/Release/obj/gen/webkit_version.h] Error 1
  </code></pre>
  
  <ul>
  <li><p>Is it possible for me to know that this exited with error? </p>
  
  <ul>
  <li><p>Does that exit with code <code>else than 0</code> since it is an error?</p></li>
  <li><p>Is that last part outputted in <code>stderr</code>?</p></li>
  </ul></li>
  </ul>
  
  <p>Considering that <code>make</code> exited with code not <code>0</code>, let's say <code>1</code>, and it output in <code>stderr</code> is it not possible for me to capture these exit codes and error message in the end?</p>
  
  <p>How can I capture the exit code and <code>stderr</code> after outputting the results of the program, and return the <code>exit code</code>/ <code>stderr</code> in the function?</p>