1

If we execute gcc to compile a source file through the system() function, how can I capture the gcc output in a string ?


I tried with popen which works as long as it reads something like "PAUSE", but did not work with gcc as it runs a new child process.

4
  • Use pipe(2) and dup2(2) before fork/exec to redirect stdin/stdout. Commented Mar 24, 2015 at 3:37
  • What output are you talking about? The compiled file? The error messages? Commented Mar 24, 2015 at 3:40
  • @rici console output. Which is I think the error messages. Commented Mar 24, 2015 at 3:42
  • Windows equivalents of pipe/dup2 are DuplicateHandle and CreatePipe. Commented Mar 24, 2015 at 3:46

2 Answers 2

1

There are many ways.

Simple way: Redirect the output to a temporary file and read the temporary file into a string.

You can use pipes. In the child process, redirect the output to pipe and read from the other end of the pipe into a string. See man pipe

//In the parent process
int fd[2]
pipes(fd);

//Use fork+execv instead of system to launch child process.
if (fork()==0) {
    //Redirect output to fd[0]
   dup2(fileno(fd[0]), STDOUT_FILENO);
   dup2(fileno(fd[0]), STDERR_FILENO);
   //Use execv function to load gcc with arguments.

} else {
  //read from the other end fd[1]
}

See thread at http://ubuntuforums.org/archive/index.php/t-1627614.html.

Also see In C how do you redirect stdin/stdout/stderr to files when making an execvp() or similar call?

For Windows, this link might help you. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499%28v=vs.85%29.aspx Flow is almost same as Linux. Syntax and primitives might be different. Here, idea is to use Pipe and redirect process output files to your pipe file.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes the simple way is pretty much what everyone thinks of in the very first moment. However, they realize that it might be simple, but certainly not the best way. On the other hand, this link's content looks quite complex. I suspected it will involve pid ids and stuff like that.
Especially complex, given the windows tag :-)
I don't like windows. But my project is stuck in windows which I believe.. is a common case for a number of people these days.
@DeltaProxy: See the edit. I hope this code layout might help you.
No problem. I still have solution.
0

If you are attempting to read the error messages produced by gcc, you need to remember that they are sent to stderr, not stdout. popen captures stdout but does not redirect stderr.

If system on Windows provides a standard posix shell, you could redirect stderr to stdout in the command line you provide to popen, by adding the redirection 2>&1.

16 Comments

OH. Good remark. That makes sense and also makes it harder to be accomplished indeed.
@DeltaProxy: On Unix, it would be trivial; you'd just do popen("gcc -o a.out file.c 2>&1", "r"); or something of that form. Maybe that will work in your environment.
It worked.. on a way. Partially. It redirected the symbol ^ as well as some space. But it must redirect the backslash for the directory of the source file, which means it will be read as an escape sequence and this follows to be a problem.
@DeltaProxy: Why would it be read as an escape? fread (or fgets) just reads the bytes which are in the file. (Except for end-of-line translation for non-binary files on Windows.)
@DeltaProxy: Also, if you have a surprisingly recent version of gcc, you should investigate the option -fno-diagnostics-show-caret
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.