I've skimmed through the $ man gcc
and $ man g++
but haven't found something that would print the error and warning counts. But I see that Emacs can print the error counts and warning counts in the *compilation*
buffer. So, there must be an option in gcc
and g++
that I'm missing, cause I don't think Emacs would itself implement error count, that's just too much work.
How do I get a count of errors and warnings from gcc and g++?
You can use the -Werror compiler flag to turn all or some warnings into errors.
gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.
You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.
You can do this by specifying Werror=warning-name , which will cause that specific warning to generate an error. For example, a warning that I promote to an error is -Wreturn-type .
It doesn't make much sense to count errors in gcc
, because usually it runs on one file at a time and a project is usually many files, so there are many invocations of gcc
to build a project. Potentially there are many errors in different files so it makes sense that whatever runs gcc
(e.g. make
, or in turn Emacs which runs make
) should keep track of the errors. Indeed, when you run M-x compile
, Emacs is the thing counting the errors. Open compile.el
(M-x find-library RET compile RET
) and look at the variable compilation-num-errors-found
, which is what gets added to the mode line (along with similar counts for warnings and infos). It's not super straightforward how those variables get updated, but ultimately it's just matching regexps on the compiler output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With