如何将gcc的错误输出保存到文件中

时间:2011-08-02 15:22:22

标签: c++ gcc

当我编译我的代码时,我得到了一堆错误,我遍及屏幕,我可以看到错误从哪里开始。如何将gcc的输出保存到文件中?

我试过像

这样的技巧
  

gcc> log.txt

或者重击结果但是没有用。搜索谷歌收益主要是解释如何使用c ++打印到文件

2 个答案:

答案 0 :(得分:16)

GCC将错误输出到标准错误流而不是标准输出流。您需要重定向标准错误,而不是标准输出。在bash:

gcc 2> log.txt

答案 1 :(得分:10)

我个人发现仅将错误输出到文件无济于事。事实上,最容易帮助我的是避免包装通常超长的错误行。所以,我决定使用vim突出显示来更好地查看错误。

没有荧光笔(View Larger Image

Screenshot - Before

使用荧光笔(View Larger Image

Screenshot - After

幸运的是,有一种非常简单的方法可以在VIM中设置新的语法高亮显示。 按照以下步骤操作,您将更有效地处理重度模板化的C ++代码:

创建新的VIM自定义语法突出显示规则集

您必须定义语法突出显示规则。将以下内容放在名为cerr.vim的文件中,并将其保存在$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

更改.vimrc文件

现在,您必须告诉vim对具有特定扩展名的文件使用新的突出显示。在我的情况下,我想将我的错误文件输出到 error.ccerr ,将以下内容放在你的主文件夹中的.vimrc中:

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

我在上面说的是,当使用VIM打开扩展名为.cerr的文件时,它们将被视为类型myerror。在第二行中,我说VIM应该使用我在上一步中为所有myerror文件定义的语法高亮规则集。

将错误输出发送到.cerr文件并使用VIM

打开它

这一步是最简单的,我们将所有错误和警告发送到error.cerr,如果文件中有任何错误,我们会立即使用VIM打开.cerr文件。

g++ failing.cc &> error.cerr || vim error.cerr