How do I capture all of my compiler's output to a file?

LinuxShellG++MakefileIo Redirection

Linux Problem Overview


I'm building an opensource project from source (CPP) in Linux. This is the order:

$CFLAGS="-g Wall" CXXFLAGS="-g Wall" ../trunk/configure --prefix=/somepath/ --host=i386-pc --target=i386-pc
$make

While compiling I'm getting lot of compiler warnings. I want to start fixing them. My question is how to capture all the compiler output in a file?

$make > file is not doing the job. It's just saving the compiler command like g++ -someoptions /asdf/xyz.cpp I want the output of these command executions.

Linux Solutions


Solution 1 - Linux

The compiler warnings happen on stderr, not stdout, which is why you don't see them when you just redirect make somewhere else. Instead, try this if you're using Bash:

$ make &> results.txt

The & means "redirect stdout and stderr to this location". Other shells often have similar constructs.

Solution 2 - Linux

In a bourne shell:

> make > my.log 2>&1

I.e. > redirects stdout, 2>&1 redirects stderr to the same place as stdout

Solution 3 - Linux

Lots of good answers so far. Here's a frill:

$ make 2>&1 | tee filetokeepitin.txt 

will let you watch the output scroll past.

Solution 4 - Linux

The output went to stderr. Use 2> to capture that.

$make 2> file

Solution 5 - Linux

Assume you want to hilight warning and error from build ouput:

make |& grep -E "warning|error"

Solution 6 - Linux

Try make 2> file. Compiler warnings come out on the standard error stream, not the standard output stream. If my suggestion doesn't work, check your shell manual for how to divert standard error.

Solution 7 - Linux

From http://www.oreillynet.com/linux/cmd/cmd.csp?path=g/gcc

> The > character does not redirect the > standard error. It's useful when you > want to save legitimate output without > mucking up a file with error messages. > But what if the error messages are > what you want to save? This is quite > common during troubleshooting. The > solution is to use a greater-than sign > followed by an ampersand. (This > construct works in almost every modern > UNIX shell.) It redirects both the > standard output and the standard > error. For instance: > > $ gcc invinitjig.c >& error-msg

Have a look there, if this helps: another forum

Solution 8 - Linux

Based on an earlier reply by @dmckee

make | tee makelog.txt

This gives you real-time scrolling output while compiling, and simultaneously write to the makelog.txt file.

Solution 9 - Linux

In C shell

  • The ampersand is after the greater-than symbol

    make >& filename

Solution 10 - Linux

It is typically not what you want to do. You want to run your compilation in an editor that has support for reading the output of the compiler and going to the file/line char that has the problems. It works in all editors worth considering. Here is the emacs setup:

https://www.gnu.org/software/emacs/manual/html_node/emacs/Compilation.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpeckerView Question on Stackoverflow
Solution 1 - LinuxJohn FeminellaView Answer on Stackoverflow
Solution 2 - LinuxNathan KiddView Answer on Stackoverflow
Solution 3 - Linuxdmckee --- ex-moderator kittenView Answer on Stackoverflow
Solution 4 - LinuxkennytmView Answer on Stackoverflow
Solution 5 - LinuxPeter NguyenView Answer on Stackoverflow
Solution 6 - LinuxDavid ThornleyView Answer on Stackoverflow
Solution 7 - LinuxGauthierView Answer on Stackoverflow
Solution 8 - LinuxeaswaranView Answer on Stackoverflow
Solution 9 - LinuxSunDontShineView Answer on Stackoverflow
Solution 10 - LinuxImmanuel LitzrothView Answer on Stackoverflow