微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 -fmax-errors=1 从旧的 gcc 版本获取完整的错误消息?

如何解决如何使用 -fmax-errors=1 从旧的 gcc 版本获取完整的错误消息?

考虑这个虚拟示例,其唯一目的是产生错误

#include <iostream>    

void foo();
void foo(int);
void foo(double); 

int main() {
    foo(std::cout);
}

gcc 11.1 的完整错误信息是

<source>: In function 'int main()':
<source>:8:8: error: no matching function for call to 'foo(std::ostream&)'
    8 |     foo(std::cout);
      |     ~~~^~~~~~~~~~~
<source>:3:6: note: candidate: 'void foo()'
    3 | void foo();
      |      ^~~
<source>:3:6: note:   candidate expects 0 arguments,1 provided
<source>:4:6: note: candidate: 'void foo(int)'
    4 | void foo(int);
      |      ^~~
<source>:4:10: note:   no kNown conversion for argument 1 from 'std::ostream' {aka 'std::basic_ostream<char>'} to 'int'
    4 | void foo(int);
      |          ^~~
<source>:5:6: note: candidate: 'void foo(double)'
    5 | void foo(double);
      |      ^~~
<source>:5:10: note:   no kNown conversion for argument 1 from 'std::ostream' {aka 'std::basic_ostream<char>'} to 'double'
    5 | void foo(double);
      |          ^~~~~~
Compiler returned: 1

对于 -fmax-errors=1,消息是相同的:https://godbolt.org/z/1zPj1Keeh

我没有对更改的版本进行二进制搜索,而是使用旧版本的 gcc(例如 6.1)在传递 -fmax-errors=1 时截断错误消息。 Full message,without -fmax

<source>: In function 'int main()':
<source>:8:18: error: no matching function for call to 'foo(std::ostream&)'
     foo(std::cout);
                  ^
<source>:3:6: note: candidate: void foo()
 void foo();
      ^~~
<source>:3:6: note:   candidate expects 0 arguments,1 provided
<source>:4:6: note: candidate: void foo(int)
 void foo(int);
      ^~~
<source>:4:6: note:   no kNown conversion for argument 1 from 'std::ostream {aka std::basic_ostream<char>}' to 'int'
<source>:5:6: note: candidate: void foo(double)
 void foo(double);
      ^~~
<source>:5:6: note:   no kNown conversion for argument 1 from 'std::ostream {aka std::basic_ostream<char>}' to 'double'
Compiler returned: 1

Truncated message with -fmax-errors=1

<source>: In function 'int main()':
<source>:8:18: error: no matching function for call to 'foo(std::ostream&)'
     foo(std::cout);
                  ^
compilation terminated due to -fmax-errors=1.
Compiler returned: 1

旧版本的 gcc 有可能在第一个(或第 n 个)错误时停止编译,但仍然输出完整的错误消息?

我意识到,我可以使用 -fmax-errors= N+1 来查看 N-th 错误的完整消息,但这并不是我想要的结果(编译实际上应该在第一个错误后停止,我不想要在输出中为第 N+1错误截断输出。)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。