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

8
当我编译我的代码时,屏幕上会出现一堆错误信息,我能看到错误从哪里开始。如何将gcc的输出保存到文件中?
我尝试了一些技巧,比如使用 "gcc > log.txt" 或者使用grep搜索结果,但都没有成功。在谷歌上搜索大多是关于如何在C++中打印到文件的解释。

你说的“没起作用”是什么意思? - Etienne de Martel
输出仍然打印在屏幕上。 - Yotam
2个回答

23

GCC将错误输出到标准错误流(standard error),而不是标准输出流(standard output)。你需要重定向标准错误流,而不是标准输出流。在bash中:

gcc 2> log.txt

1
在csh或tcsh中:gcc ... >& log.txt(将stdout和stderr都重定向到log.txt,但gcc实际上并不会写太多到stdout)。 - Keith Thompson

10

我个人发现仅仅将错误输出到文件是不够的。事实上,能够帮助我的最简单的方法是避免包装通常非常长的错误行。因此,我决定使用vim高亮来更好地查看错误。

没有高亮器(查看较大图片

屏幕截图-之前

使用高亮器(查看较大图片

屏幕截图-之后.

幸运的是,在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。第二行中我表示,对于所有myerror文件,VIM应该使用我在前一步中定义的语法高亮规则集。

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

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

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

我的新解决方案是使用“sublime_text”。我已经写了一个快速教程,介绍如何在这里设置它。 - Mani Zandifar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接