0

I am compiling a C++ program with g++ and each time get a huge number of errors, forcing me to scroll up every time I want to view the first (and most relevant) error. I am wondering if there is an option when I am compiling the program that would allow me to limit the number of error messages displayed in the terminal.

  • How are you compiling it? – Jeff Schaller Mar 26 '18 at 1:47
  • It seems that you are asking about IDE. – Yves Mar 26 '18 at 2:43
  • g++ -std=c++11 main.cpp – Matt Mclaughlin Mar 26 '18 at 7:02
0

Use compiling-command | head --lines 32 to output first 32 lines from compiling-command output.
You can also use compiling-command | grep "Text to search" | head --lines 32 to display first 32 finds of "Text to search".

Disabling -Wall option with gcc will not output as many errors.

1

You can use option -fmax-errors=1 to set the number of errors to 1 and so on. The default 0 is infinite. The gcc option applies to g++.

1

You have several options:

  • Use compiler options to set a limit on the number of error messages before the compiler just gives up completely, as in another answer.
  • Use head, remembering that error messages are sent to standard error of course. Or use one of the many tools floating around that post-process GCC output. Also remember that with modern versions of GCC that have adopted the clang model a single error can span several lines, so low line counts for head might not be very useful at all.
  • Use a paginator such as pager, pg, more, or less, again remembering that error messages are sent to standard error.
  • Run the compilation command in a :terminal within NeoVIM, which provides quite a lot of scrollback, and of course all of the usual VIM navigation key sequences for quickly going to the top of the output and searching.
  • Integrate with your text editor.

The latter is the most interesting option. It comes in at least two flavours:

  • Write the standard output+standard error of the compiler to a file and tell your text editor to treat it as an errors file. With Vim and NeoVIM this is done with
    vim -q errors_file
    and
    nvim -q errors_file
    where you can substitute the name of your choosing for errors_file of course.

    This will open the text editor with the errors file loaded into the quickfix buffer, and the file for the first error or warning message opened and positioned at the line number (and column) of that error.

  • Run the compiler from within one's text editor. With Vim and NeoVIM if one has a makefile one can invoke it with the :make command, and VIM/NeoVIM will put the error output from running make into a quickfix list, again automatically positioning at the file, line, and column of the first error or warning message.

With both of these, one can of course move from the location of one warning/error to the next using :cnext and the various other Vim/NeoVIM quickfix list navigation commands listed at :help :cc. Note that there's no reason to limit the number of errors with these mechanisms, and there are Vim/NeoVIM commands to return to the position of the current message, or go back to the position of the first message.

With the latter specifically, one can also run :make multiple times and change back and forth between older and newer error lists using :colder and :cnewer. (One can additionally change :make to run something other than make, using set makeprg, more of which in the VIM/NeoVIM manual.)

One can do similar things with emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.