2011-08-02 35 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) –