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.