2010-06-04 44 views
3

我有一個很大的代碼庫,我的任務是移植到64位。代碼編譯,但它打印了大量不兼容的指針警告(如預期的那樣)。有什麼辦法可以讓gcc打印發生錯誤的行嗎?在這一點上,我只是使用gcc的錯誤消息來追蹤需要修改的假設,並且不得不查找每一個都不好玩。當它發出錯誤時,有沒有辦法讓gcc打印違規行?

+1

......我非常確定GCC *會*寫出發現問題的行和文件。 (例如「variant.hpp:1140:warning:聲明'哪個'會隱藏'this'的成員,意味着警告位於文件variant.hpp,位於第1140行。) – 2010-06-04 18:30:33

回答

2

我公然爲此偷走了Joseph Quinsey的answer。唯一的區別是,我試圖使代碼更易於理解:

對於bash,使用make 2>&1 | show_gcc_lineshow_gcc_line下面的腳本:

#!/bin/bash 
# Read and echo each line only if it is an error or warning message 
# The lines printed will start something like "foobar:123:" so that 
# line 123 of file foobar will be printed. 

while read input 
do 
    loc=$(echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p') 
    len=${#loc} 
    file=${loc% *} 
    line=${loc#* } 

    if [ $len -gt 0 ] 
    then 
     echo "$input" 
     echo "$(sed -n ${line}p $file)" 
     echo 
    fi 
done 

這部分是因爲我不喜歡的格式原版的。這隻會打印警告/錯誤,其次是導致問題的代碼行,然後是空白行。我也刪除了連字符串。

+0

+1但也許應該完成,例如,在Python中? _Shell_腳本顯然是_passe_。 – 2010-06-06 22:44:20

+0

另外,通常,您需要使用類似** find **的內容,因爲Makefiles經常更改目錄。我已經假定一切都位於頂層目錄或者低於頂層目錄。 – 2010-06-06 22:50:12

+0

當然,除非OP的遺留代碼編碼器已經閱讀_Recursive Make Considered Harmful_ http://aegis.sourceforge.net/auug97.pdf – 2010-06-06 23:12:23

1

使用-W選項可控制要顯示哪些警告。該參數解釋爲here

此外可以使用該特技抑制逐行輸出:

gcc ... 1>/dev/nul 
+0

嗯,我希望能夠查看源代碼行本身的錯誤。 – Alex 2010-06-04 18:53:35

0

由編譯器發出錯誤消息,實際的源極線長了,(特別是在C)的時間 - 它已被轉化的到一個標記流,然後到一個抽象語法樹,然後到一個裝飾的語法樹...... gcc已經有足夠多的編譯步驟,所以它故意不包含重新打開文件和重新獲取文件的功能原始來源。這就是編輯們的目的,而且幾乎所有人都有命令開始編譯並在按鍵時跳到下一個錯誤。幫你一個忙,並使用現代編輯器來瀏覽錯誤(甚至可以半自動地修復它們)。

0

這個小腳本應該可以工作,但我現在無法測試它。抱歉,如果需要編輯。

LAST_ERROR_LINE=`(gcc ... 2>&1 >/dev/null) | grep error | tail -n1` 
FILE=`echo $LAST_ERROR_LINE | cut -f1 -d':'` 
LINE=`echo $LAST_ERROR_LINE | cut -f2 -d':'` 
sed -n "${LINE}p" $FILE 
2

也許一個腳本來打印所需的行將有所幫助。如果你正在使用CSH(!不太可能)使用方法:

make ... |& show_gcc_line 

show_gcc_line下面的腳本:

#!/bin/csh 
# Read and echo each line. And, if it starts with "foobar:123:", print line 123 
# of foobar, using find(1) to find it, prefaced by ---------------. 

set input="$<" 
while ("$input") 
    echo "$input" 
    set loc=`echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p'` 
    if ($#loc) then 
     find . -name $loc[1] | xargs sed -n $loc[2]s/^/---------------/p 
    endif 
    set input="$<" 
end 

而對於慶典,使用make ... 2>&1 | show_gcc_line有:

#!/bin/bash 
# Read and echo each line. And, if it starts with "foobar:123:", print line 123 
# of foobar, using find(1) to find it, prefaced by ---------------. 

while read input 
do 
    echo "$input" 
    loc=$(echo "$input" | sed -n 's/^\([^ :]*\):\([0-9]*\):.*/\1 \2/p') 
    if [ ${#loc} -gt 0 ] 
    then 
     find . -name ${loc% *} | xargs sed -n ${loc#* }s/^/---------------/p 
    fi 
done 
0

對我來說,重定向錯誤信息的符號很難記住。

所以,這裏是我的版本,打印出來的gcc錯誤信息:

$ee make 

兩個錯誤和警告消息:

ee2 make 

如何: 添加到這些。bashrc

function ee() { 
    $* 2>&1 | grep error 
} 

function ee2() { 
    $* 2> ha 
    echo "-----" 
    echo "Error" 
    echo "-----" 
    grep error ha 
    echo "-------" 
    echo "Warning" 
    echo "-------" 
    grep warning ha 
} 
相關問題