2011-08-02 30 views
5

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

我尝试的技巧,比如

GCC> log.txt的

或grepping的结果,但没有奏效。谷歌搜索收益率说明如何打印不与C++

+0

你是什么意思“没有工作”? –

+0

输出仍然打印在屏幕上 – Yotam

回答

16

GCC输出错误文件到标准误差流至标准输出流大多导致。您需要重定向标准错误,而不是标准输出。在bash中:

gcc 2> log.txt 
+1

并在csh或tcsh中:'gcc ...>&log.txt'(它将stdout和stderr指向'log.txt',但gcc不会为stdout写入太多内容无论如何)。 –

9

我个人发现只是将错误输出到文件中不会有帮助。事实上,可以帮助我的最简单的事情是避免包装通常超长的错误行。所以,我决定使用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 

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

发送你的错误输出到.cerr文件,并用VIM打开它

这一步是最简单的,我们把所有的错误和警告error.cerr,如果有文件中的任何错误,我们马上开使用VIM的.cerr文件。

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

我的新解决方案是使用''sublime_text''。我写了一个关于如何在[这里]设置它的快速教程(http://stackoverflow.com/questions/13674223/how-do-you-get-vim-to-highlight-c-syntax-errors-like - 视觉工作室/ 21895852#21895852) –