如何快速获取终端中的第一个编译器错误消息?



我觉得这是一个常见的问题,但我找不到任何东西。很多时候,当我编译一个程序时,我会有一长串编译错误,我必须在终端中向上滚动才能找到第一个错误。这有点乏味,有时我会滚动浏览第一个错误而没有注意到。有没有更快的方法来导航它?

特定于终端的方法:

$ clear && make

然后使用快捷键Shift+Home跳到顶部。此快捷方式可能不适用于所有主机。它似乎适用于侏儒终端和年龄。它似乎不适用于 xterm,但我假设此类控制台可以配置为添加快捷方式(例如,对于 xterm,请参阅链接(。

特定于编译器的方法:

或者,可以使用编译器机制来限制要显示的错误数。clang 和 gcc 都支持-Wfatal-errors在第一个错误时退出(不要与将警告变成错误的-Werror混淆(。从 gccman页面:

-Wfatal-errors
This option causes the compiler to abort compilation on the first
error occurred rather than trying to keep going and printing
further error messages.

在某些情况下,在第一个错误时退出可能没有帮助(即,当缺少某些标头时,在错误告诉您哪个标识符未定义之前,可能会有一个关于缺少;的错误(。 因此,限制错误数可能更有帮助。对于 gcc,最多显示n错误是-fmax-errors=n(对于 clang-ferror-limit=n(。您可以将其调整为较小的数字,这样您就可以一次查看所有错误而无需滚动。

-fmax-errors=n
Limits the maximum number of error messages to n, at which
point GCC bails out rather than attempting to continue
processing the source code.  If n is 0 (the default), there is
no limit on the number of error messages produced.  If
-Wfatal-errors is also specified, then -Wfatal-errors takes
precedence over this option.

相关内容

最新更新