2010-06-04 63 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 
} 
相关问题