How to save the error output of gcc to file

19,713

Solution 1

GCC outputs errors to the standard error stream not to the standard output stream. You need to redirect standard error, instead of standard output. In bash:

gcc 2> log.txt

Solution 2

I personally found out that merely outputing the error to a file would not help. In fact the easiest thing that could help me was to avoid wrapping the error lines which are usually super long. So, I decided to use vim highlighting to see the errors better.

Without the highlighter (View Larger Image)

Screenshot - Before

With the highlighter (View Larger Image)

Screenshot - After.

And fortunately, there was a very easy way to set up a new syntax highlighting in VIM. Follow these steps and you will be more productive working on heavily templated C++ codes:

Create a new VIM custom syntax highlighting rule set

you have to define the syntax highlighting rules. Put the following in a file called cerr.vim and keep it for example in $HOME/vim_syntax/cerr.vim

"Set line wrapping to off to see more error lines in one page
set nowrap                   
set showmatch
"I use stl and boost alot so it is good to remove the namespaces from the error file :)
silent! %s/st![enter image description here][2]d:://g                                                
silent! %s/boost::fusion:://g                                                  
silent! %s/boost:://g                                                
"Usually I am not interested in the file paths until I can locate the error so I tried to
"hide them
silent! %s/\/[^\.]*\//   /g                                                    
"By default syntax highlighting for each line is limited to 3000 characters    
"However, 3000 characters is not sufficient for lengthy C++ errors, so I change it to 20000
set synmaxcol=20000                                                            
"Now I define the keywords that I would like them to be highlighted
syn keyword cerrInfo instantiated                                             
syn keyword cerrError error Error ERROR                                       
syn keyword cerrWarning warning Warning WARNING
                           
"-------------------------------------                                         
"In this step I would like to distinguish the prefix in each line (which shows the file name) from the rest of the line
syn region cerrLine start=/^/ end=/\:/                                        
syn region cerrSeparator start=/^\.+/ end=/\./ fold oneline

"I want to make templated type information less visible while debugging              
"You have to remember that a type can have nested types. So I define three regions
syn region cerrTemplate1 matchgroup=xBracket1 start=/</ end=/>/ contains=cerrTemplate2 fold oneline
syn region cerrTemplate2 matchgroup=xBracket2 start=/</ end=/>/ contains=cerrTemplate3 fold contained oneline
syn region cerrTemplate3 start=/</ end=/>/ contains=cerrTemplate3 contained oneline fold oneline

"Now I would like to highlight whatever is in parenthesis with a different color so I make
"another region in here. This makes sure that function arguments can have different color            
 syn region cerrPar matchgroup=xBracket start=/(/ end=/)/ contains=cerrTemplate1 oneline fold
"GCC puts the real type information in brackets, let's group them separately
 syn region cerrBracket start=/\[/ end=/\]/ contains=cerrTemplate1,cerrPar oneline

"Again GCC puts the error in these weird characters :) So I define a separate region here
syn region cerrCode start=/‘/ end=/’/ contains=cerrPar,cerrBracket,cerrTemplate1 oneline

"And finally I would like to color the line numbers differently
syn match   cerrNum "[0-9]\+[:|,]"                                            

"--------------------------------------------------------------------------
"Now the fun part is here, change the colors to match your terminal colors. 
"I Use the following colors for my white background terminal.
"In the following we assign a color for each group that we defined earlier

"Comment is a default VIM color group
highlight link cerrInfo Comment     
"We use custom coloring for the rest                                          
highlight default cerrWarning ctermfg=red ctermbg=yellow                      
highlight default cerrError ctermfg=white ctermbg=red                         
highlight default cerrLine ctermfg=grey term=bold                             
highlight default cerrSeparator ctermfg=darkgrey                              
highlight default cerrTemplate1 ctermfg=grey term=bold                        
highlight default cerrTemplate2 ctermfg=grey term=bold                        
highlight default cerrTemplate3 ctermfg=grey                                  
highlight default cerrCode cterm=bold ctermfg=darkgrey                        
highlight default cerrBracket ctermfg=darkgreen                               
highlight default xBracket1 ctermfg=darkgrey term=bold                         
highlight default xBracket2 ctermfg=darkgrey                                   
highlight default cerrPar ctermfg=yellow                                      
highlight default cerrNum ctermfg=red

Change your .vimrc file

Now, you have to tell vim to use your new highlighting for files with specific extension. In my case I would like to output my error files to error.ccerr, put the following in your .vimrc in your home folder:

au BufRead,BufNewFile *.cerr set filetype=myerror
au Syntax myerror source $HOME/vim_syntax/cerr.vim  

What I say in the above is that when files with the extension .cerr are opened using VIM, they will be considered of type myerror. In the second line I am saying that VIM should use my syntax highlighting rule set which I defined in the previous step for all myerror files.

Send your error output to a .cerr file and open it with VIM

This step is the easiest, we send all errors and warning to error.cerr and if there is any error in the file we immediately open the .cerr file using VIM.

g++ failing.cc &> error.cerr || vim error.cerr
Share:
19,713
Yotam
Author by

Yotam

Updated on June 15, 2022

Comments

  • Yotam
    Yotam almost 2 years

    When I compile my code I get a bunch of errors which I span through the screen and I can see where does the error start. How can I save the output of gcc to a file?

    I tried tricks like

    gcc > log.txt

    or grepping the result but it didn't work. Searching google yields mostly result on explaining how to print to file with c++

    • Etienne de Martel
      Etienne de Martel almost 13 years
      What do you mean "didn't work"?
    • Yotam
      Yotam almost 13 years
      The output is still printed on the screen
  • Keith Thompson
    Keith Thompson over 10 years
    And in csh or tcsh: gcc ... >& log.txt (which directs both stdout and stderr to log.txt, but gcc doesn't write much to stdout anyway).
  • Mani Zandifar
    Mani Zandifar about 10 years
    My newer solution is to use sublime_text. I have written a quick tutorial of how to set it up in here