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

107,412

Solution 1

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

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

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

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

make |& grep -E "warning|error"

Solution 5

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

$make 2> file
Share:
107,412
pecker
Author by

pecker

Updated on July 08, 2022

Comments

  • pecker
    pecker almost 2 years

    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.

  • ephemient
    ephemient about 14 years
    I think that the return code of a pipeline is usually the return code of the last command; in this case, tee not make. So the || more unfortunately doesn't do what you say it does. Compare false || echo $? to false | cat || echo $?.
  • ephemient
    ephemient about 14 years
    This is equivalent to make > results.txt 2>&1 but with less typing :)
  • Dana the Sane
    Dana the Sane about 14 years
    Is there an equivalent for pipes?
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten about 14 years
    @ephemient: Ah. You are right....heck, I made that work once, and now I can't recall how. In anycase, I use tee in this application fairly often.
  • ephemient
    ephemient about 14 years
    @Dana the Sane: Bash 4 has |& for pipes, which is equivalent to 2>&1 |.
  • ephemient
    ephemient about 14 years
    shopt -s pipefail, perhaps?
  • CosmicGiant
    CosmicGiant almost 8 years
    While code-only answers are not forbidden, please understand that this is a Q&A community, rather than a crowd-sourcing one, and that, usually, if the OP understood the code being posted as an answer, he/she would have come up with a similar solution on his/her own, and wouldn't have posted a question in the first place. As such, please provide context to your answer and/or code by explaining how and/or why it works.
  • Piyush Soni
    Piyush Soni about 7 years
    Please note that >&, &> and 2>&1 are all shell dependent, and may work or not depending on if you have bash/csh or other shells. @Alberto: I was confused too, because I was using csh and &> didn't work for me while >& worked. This was the reason.
  • FractalSpace
    FractalSpace about 6 years
    This does not answer 'How do I capture all of my compiler's output to a file? '
  • 2785528
    2785528 almost 6 years
    When you compile within emacs, all the compiler output is captured in the * compilation * buffer of the editor. Saving this buffer to a file is then, in the emacs style, a single command, C-x Cw (and choose a file name).
  • Steven Lee
    Steven Lee over 2 years
    @PiyushSoni: I was using csh and &> didn't work while >& worked.