c++ - Can't invoke g++ with redirected stdout -


i'm looking invoke g++ , output. here's code:

#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <iostream> #include <boost/optional.hpp> #include <vector> #include <string>  namespace util {     template<typename t>     using optional = boost::optional<t>; }  namespace wide {     namespace driver     {         struct processresult         {             std::string std_out;             int exitcode;         };          processresult startandwaitforprocess(std::string name, std::vector<std::string> args, util::optional<unsigned> timeout);     } }  #include <unistd.h>     #include <sys/types.h>     #include <sys/wait.h>  #include <iostream> #include <fcntl.h> wide::driver::processresult wide::driver::startandwaitforprocess(std::string name, std::vector<std::string> args, util::optional<unsigned> timeout) {     int filedes[2];     pipe(filedes);     pid_t pid = fork();     if (pid == 0) {         while ((dup2(filedes[1], stdout_fileno) == -1) && (errno == eintr)) {}         auto fd = open("/dev/null", o_rdwr);         while ((dup2(fd, stdin_fileno) == -1) && (errno == eintr)) {}         //freopen("/dev/null", "rw", stdin);         //freopen("/dev/null", "rw", stderr);         //close(filedes[1]);         close(filedes[0]);         std::vector<const char*> cargs;         cargs.push_back(name.c_str());         (auto&& arg : args)             cargs.push_back(arg.c_str());         cargs.push_back(nullptr);         execv(name.c_str(), const_cast<char* const*>(&cargs[0]));     }     std::string std_out;     close(filedes[1]);     char buffer[4096];     while (1) {         ssize_t count = read(filedes[0], buffer, sizeof(buffer));         if (count == -1) {             if (errno == eintr) {                 continue;             } else {                 perror("read");                 exit(1);             }         } else if (count == 0) {             break;         } else {             std_out += std::string(buffer, buffer + count);         }     }     close(filedes[0]);     int status;     processresult result;     result.std_out = std_out;     waitpid(pid, &status, 0);     if (!wifexited(status))         result.exitcode = 1;     else {         result.exitcode = wexitstatus(status);         if (result.exitcode != 0) {             std::cout << name << " failed code " << result.exitcode << "\n";         }     }     return result; }  int main() {     auto r = wide::driver::startandwaitforprocess("g++", { "-std=c++14", "main.cpp" }, 150);     std::cout << r.std_out << "!!!!\n!!!!\n" << r.exitcode << "\n"; } 

the output:

read: bad file descriptor g++ failed code 1 !!!! !!!! 1 

just invoke g++ main.cpp -std=c++14 && ./a.out.

i've used strace doesn't give more interesting details- process runs, fork/exec, above error. can invoke other processes above code don't know what's different g++. can invoke gcc popen without problems don't know what's different here.

the error here not helpful. how can invoke g++ , output?

the problem here call execv requires full path executable first argument.

what need execvp uses contents of path environment variable find executable, , requires name g++.


Comments