2011-08-02 24 views
5

Khi tôi biên dịch mã, tôi nhận được một loạt lỗi mà tôi trải qua màn hình và tôi có thể thấy lỗi bắt đầu từ đâu. Làm thế nào tôi có thể lưu đầu ra của gcc vào một tệp?Cách lưu đầu ra lỗi của gcc thành tệp

tôi đã cố gắng thủ thuật như

gcc> log.txt

hoặc grepping kết quả nhưng nó đã không làm việc. Tìm kiếm google sản lượng chủ yếu là kết quả trên giải thích làm thế nào để in vào tập tin với C++

+0

Ý của bạn là "không hoạt động"? –

+0

Đầu ra vẫn được in trên màn hình – Yotam

Trả lời

16

GCC ra lỗi vào dòng tiêu chuẩn lỗi không đến chuẩn đầu ra suối. Bạn cần chuyển hướng lỗi chuẩn, thay vì đầu ra tiêu chuẩn. Trong bash:

gcc 2> log.txt 
+1

Và trong csh hoặc tcsh: 'gcc ...> & log.txt' (chỉ dẫn cả stdout và stderr đến' log.txt', nhưng gcc không viết nhiều để stdout dù sao). –

9

Cá nhân tôi phát hiện ra rằng chỉ việc xuất ra lỗi cho tệp sẽ không hữu ích. Trong thực tế, điều đơn giản nhất có thể giúp tôi là tránh việc bao bọc các dòng lỗi thường là siêu dài. Vì vậy, tôi quyết định sử dụng vim làm nổi bật để xem các lỗi tốt hơn.

Nếu không có sự highlighter (View Larger Image)

Screenshot - Before

Với highlighter (View Larger Image)

Screenshot - After.

Và may mắn thay, có một cách rất dễ dàng để thiết lập cú pháp mới làm nổi bật trong VIM. Thực hiện theo các bước sau và bạn sẽ có năng suất cao hơn làm việc trên nhiều templated mã C++:

Tạo một quy tắc cú pháp VIM tùy chỉnh làm nổi bật mới thiết

bạn phải xác định cú pháp nêu bật quy tắc. Đặt sau đây trong một tập tin gọi cerr.vim và giữ cho nó ví dụ như trong $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 

Thay đổi tập tin vimrc của bạn

Bây giờ, bạn phải nói với vim để sử dụng làm nổi bật mới của bạn cho các tập tin với phần mở rộng cụ thể . Trong trường hợp của tôi, tôi muốn đầu ra các file lỗi của tôi để error.ccerr, đặt sau đây trong vimrc của bạn trong thư mục chính của bạn:

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

Những gì tôi nói ở trên là khi tập tin với phần mở rộng .cerr được mở bằng VIM, chúng sẽ được xem là loại myerror. Trong dòng thứ hai tôi nói rằng VIM nên sử dụng bộ quy tắc tô sáng cú pháp của tôi mà tôi đã xác định trong bước trước đó cho tất cả các tệp myerror.

Gửi ra lỗi của bạn vào một tập tin .cerr và mở nó với VIM

Bước này là đơn giản nhất, chúng tôi gửi tất cả các lỗi và cảnh báo cho error.cerr và nếu có bất kỳ lỗi trong tập tin chúng tôi ngay lập tức mở tệp .cerr bằng VIM.

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

Giải pháp mới hơn của tôi là sử dụng '' sublime_text''. Tôi đã viết một hướng dẫn nhanh về cách thiết lập nó trong [ở đây] (http://stackoverflow.com/questions/13674223/how-do-you-get-vim-to-highlight-c-syntax-errors-like -visual-studio/21895852 # 21895852) –

Các vấn đề liên quan