Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortening GCC error messages

Tags:

c++

gcc

Whenever gcc can't find a matching overload for a function with multiple overloads, it gives lines and lines of errors, explaining which overload was tried and why it was not used.

While often it is useful, it is also often not, as the problem as a simple typo at the call site. And in this particular case, it is not even helpful, because it will take considerable time to even find out which line is ultimately responsible for this issue.

Is there any command line switch to GCC to shorten the output and only include the actual triggering line? For example:

#include <string>
#include <iostream>

struct Z{};

void foo() {

    std::string s;
    Z z;

    std::cout << z; // typo - meant s 
}

See error output: https://godbolt.org/g/wz5vL2

Small addition: third party solutions (STLFilt, gccfilter, etc) do not fit the bill, because a) my work environment is not welcoming towards installing 3rd party apps and b) they tend to become unmaintained and stop working with the next compiler upgrade

like image 459
SergeyA Avatar asked Oct 19 '25 13:10

SergeyA


1 Answers

One way is to use -Wfatal-errors. It changes the error message from

<source>: In function 'void foo()':

<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')

     std::cout << z; // typo - meant s

     ~~~~~~~~~~^~~~

In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/iostream:39:0,

                 from <source>:2:

/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]

       operator<<(__ostream_type& (*__pf)(__ostream_type&))

many more lines of errors

to

    <source>: In function 'void foo()':

<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')

     std::cout << z; // typo - meant s

     ~~~~~~~~~~^~~~

compilation terminated due to -Wfatal-errors.

Compiler returned: 1

The only downside is you will only get the first error. If your compile times are long then this isn't the greatest as you wouldn't be able to fix any other errors until you fix that first one and recompile.

like image 173
NathanOliver Avatar answered Oct 21 '25 02:10

NathanOliver