5

我在解释 gcc (4.8.2) 警告和错误时遇到问题。更准确地说,很难分辨一个问题在哪里结束,另一个问题从哪里开始。我只能通过控制台访问构建机器,因此不能选择使用 IDE。

我真的需要能够快速区分个别问题。有没有办法让 GCC 在不同的诊断消息之间插入一些东西?

这是我得到的示例输出:

/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp: In member function ‘virtual bool xmodel::Core::DataItemCollection::tryAdditionalItemRegistration(std::shared_ptr<xmodel::Core::IDataItem>) const’:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&, const QUuid&)’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note: candidates are:
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
                 from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note:   template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note:   couldn't deduce template parameter ‘_Func’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
                 from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note:   template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note:   couldn't deduce template parameter ‘_Result’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:417:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^

实际上,这还不算太糟糕,因为没有涉及宏扩展(我们也使用 Boost.Preprocessor)。尽管如此,很难一眼看出这个输出是关于两个问题的,第二个在最后 3 行,第一个占据了之前的整个文本墙。

有没有办法在它们之间插入一些分隔符?即使是空行也可以,您可以搜索或grep搜索。

我已经阅读了关于 Options to Control Diagnostic Messages Formatting 的 GCC 文档,但我找不到任何东西。[也不能用作搜索锚,因为并非所有消息都有一个(上面的第一个没有)。我没主意了。

4

3 回答 3

5

您可以尝试 vim 及其快速修复功能。我不知道你对 vim 有多熟悉,但它的:make命令运行构建并捕获产生的错误,然后使用:cnext(或更短的:cn)你遍历所有错误,编辑器跳转到包含错误的文件+行。上面的链接中记录了许多其他功能,但这基本上就是它的要点。

于 2014-02-20T20:46:17.087 回答
5

分析

我正在使用其他人编写的一些代码(准确地说,正在调试关于 SO 的另一个问题),并且我取消了-Werror获取警告的选项:

$ gcc -O3 -g -std=c11 -Wall -Wextra stl.c -o stl
stl.c: In function ‘printStudent’:
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
     printf("Name: %s ", student->first);
     ^
stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
     printf("Grade: %s\n", student->grade);
     ^
stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
$

并启用-Werror以获取错误消息:

$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror stl.c -o stl
stl.c: In function ‘printStudent’:
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
     printf("Name: %s ", student->first);
     ^
stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
     printf("Grade: %s\n", student->grade);
     ^
stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
cc1: all warnings being treated as errors
$

您会收到一些“上下文消息”,例如In function消息,但是每个单独的警告都以 开头<source file>:<line>:<col>: warning:,并且每个错误都以包含error代替的行开头warning。它继续到下一条上下文消息,或下一条警告或错误消息,或最后的摘要行(“所有警告都被视为错误”)。如果您有额外的信息正在生成(-H-v选项),那么生活会更复杂,但对于例行编译来说,它涵盖了它。

在您的更广泛的 C++ 示例中,您有一个上下文消息和两个错误,并带有多个注释来解释为什么会产生第一个错误。

合成

因此,可以使用合适的标记来拆分消息。事实上,它可以很容易地用一个简单的sed脚本来完成:

#!/bin/sh
#
# Insert markers before GCC warning and error messages

sed -e '
/^.*:[1-9][0-9]*:[1-9][0-9]*: warning: /i\
\
-- WARNING --\
\

/^.*:[1-9][0-9]*:[1-9][0-9]*: error: /i\
\
-- ERROR --\
\
' "$@"

该脚本在您的诊断输出中发现了第二个错误,我第一眼就错过了。

例子

这是您的示例和我的示例的一些复合输出(加上未删减的编译命令行,杂项):

/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp: In member function ‘virtual bool xmodel::Core::DataItemCollection::tryAdditionalItemRegistration(std::shared_ptr<xmodel::Core::IDataItem>) const’:

-- ERROR --

/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: error: no matching function for call to ‘bind(<unresolved overloaded function type>, const std::_Placeholder<1>&, const QUuid&)’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note: candidates are:
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
                 from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__or_<std::is_integral<typename std::decay<_Tp>::type>, std::is_enum<typename std::decay<_Tp>::type> >::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1655:5: note:   template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note:   couldn't deduce template parameter ‘_Func’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^
In file included from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/bits/stl_algo.h:66:0,
                 from /gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/algorithm:62,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qglobal.h:68,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qatomic.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/qhash.h:45,
                 from /gl-sync/thirdparty/Qt/4.8.4/lnx_x23.64/include/QtCore/QHash:1,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/rappserv/code/include/convenience/stdhash_quuid.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/ItemId.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItem.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/DataItemDescriptor.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/include/xmodel/core/IDataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.h:11,
                 from /usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:9:
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/gl-sync/compilers/gcc/4.8.2/sle11sp0/include/c++/4.8.2/functional:1682:5: note:   template argument deduction/substitution failed:
/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:414:66: note:   couldn't deduce template parameter ‘_Result’
       , std::bind(&IDataItemCollectionScope::findItemById, _1, id)
                                                                  ^

-- ERROR --

/usr2/viewstore_some/xy01/xy01_unix1/fubar/extensions/xmodel/core/code/src/DataItemCollection.cpp:417:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
'stl' is up to date.
    gcc -O3   -g   -I/Users/jleffler/inc   -std=c11   -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition     stl.c -o stl -L/Users/jleffler/lib/64 -ljl
    gcc -O3   -g   -I/Users/jleffler/inc   -std=c11   -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition     stl.c -o stl -L/Users/jleffler/lib/64 -ljl
stl.c: In function ‘printStudent’:

-- WARNING --

stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]
     printf("Name: %s ", student->first);
     ^

-- WARNING --

stl.c:84:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Wformat=]

-- WARNING --

stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
     printf("Grade: %s\n", student->grade);
     ^

-- WARNING --

stl.c:85:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
    gcc -O3   -g   -I/Users/jleffler/inc   -std=c11   -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror    stl.c -o stl -L/Users/jleffler/lib/64 -ljl
stl.c: In function ‘printStudent’:

-- ERROR --

stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]
     printf("Name: %s ", student->first);
     ^

-- ERROR --

stl.c:84:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘List’ [-Werror=format=]

-- ERROR --

stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
     printf("Grade: %s\n", student->grade);
     ^

-- ERROR --

stl.c:85:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Werror=format=]
cc1: all warnings being treated as errors
于 2014-03-29T23:49:43.207 回答
2

您的问题有多种解决方案。

首先要问的是,你愿意把GCC升级到4.9吗?如果是,那就太好了,因为 GCC 4.9 原生支持诊断输出着色:

-fdiagnostics-color[=WHEN]
-fno-diagnostics-color

您可以在此处找到有关使用的更多信息。

另一方面,如果你想坚持使用 GCC 4.8.2,你有以下选择:

  • 颜色GCC

    colorgcc 是由 Jamie Moyers 编写的 Perl 脚本,用于为 GCC 的终端输出着色,以便可以在较长的编译器输出中找到错误消息。

  • 吉尔CC

    GilCC 是一个 Ruby 程序,可以为任何编译器(例如 GCC)的输出着色。因此,如果您是 GCC 用户,您可能会意识到很难从其他文本中分辨出“警告”和“错误”。GilCC 是为了解决这个问题而编写的。

  • gccfilter(更多针对 g++)

    gccfilter 是一个过滤器,用于对 gcc 诊断消息进行着色和简化(或扩展)。gccfilter 特别针对 g++(即关于 C++)消息,这些消息可能包含许多难以理解的与模板相关的错误或警告。

于 2014-04-02T08:12:10.240 回答