70

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.

4
  • 1
    Best practice recommended by the autoconf maintainers is to execute your first command differently: use '../trunk configure CFLAGS="-g Wall" CXXFLAGS="-g Wall" ...'. That is, don't set C{XX,}FLAGS in the environment, but rather as arguments to configure. Commented Feb 19, 2010 at 16:14
  • @William Pursell, I believe pecker is, the '$' dollar sign I believe is being used to represent the shell prompt, due to the its usage on the second line ($make), simply without the typically space after the shell prompt.
    – mctylr
    Commented Feb 19, 2010 at 17:28
  • @mctylr "$ FOO=x cmd" is very different from "$ cmd FOO=x". In the former, cmd is run with FOO set to "x" in the environment. In the latter, the string "FOO=x" is an argument to cmd. Commented Feb 20, 2010 at 5:35
  • Agreed. Given the possible newness of the questioner, I was trying to be gentle.
    – mctylr
    Commented Feb 20, 2010 at 6:05

11 Answers 11

131

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.

5
  • 7
    This is equivalent to make > results.txt 2>&1 but with less typing :)
    – ephemient
    Commented Feb 19, 2010 at 16:19
  • 1
    Is there an equivalent for pipes? Commented Feb 19, 2010 at 16:20
  • 4
    @Dana the Sane: Bash 4 has |& for pipes, which is equivalent to 2>&1 |.
    – ephemient
    Commented Feb 19, 2010 at 17:33
  • 3
    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. Commented Feb 6, 2017 at 8:48
  • @PiyushSoni: I was using csh and &> didn't work while >& worked.
    – lhchuopp
    Commented Dec 6, 2021 at 11:24
29

In a bourne shell:

make > my.log 2>&1

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

1
  • why executing make 2>&1 > my.log doesn't have the same behavior? Commented Jan 23 at 14:24
28

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

$ make 2>&1 | tee filetokeepitin.txt 

will let you watch the output scroll past.

4
  • 1
    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
    Commented Feb 19, 2010 at 16:17
  • @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. Commented Feb 19, 2010 at 16:50
  • shopt -s pipefail, perhaps?
    – ephemient
    Commented Feb 19, 2010 at 17:34
  • or; "set -o pipefail"
    – Erik
    Commented Mar 10 at 13:43
7

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

$make 2> file
6

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

make |& grep -E "warning|error"
1
  • 1
    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. Commented Jul 19, 2016 at 19:25
4

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.

3

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.

3

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

0
2

In C shell - The ampersand is after the greater-than symbol

make >& filename
2

The below command logs the output of the 'make' command into the file 'gnu_make.log'

script gnu_make.log -c "make"

This file may contain line termination characters in dos format, to remove that the following command may be used.

dos2unix gnu_make.log

The file also may contain color-escape sequences, which works fine when viewed using 'cat' command, but if one wishes to remove those, it may be done using the below command

sed -i $'s/\e\\[[0-9;:]*[a-zA-Z]//g' gnu_make.log
0

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

2
  • This does not answer 'How do I capture all of my compiler's output to a file? ' Commented Mar 28, 2018 at 13:29
  • 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).
    – 2785528
    Commented Jun 25, 2018 at 21:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.