Show different level of warnings in GCC compiler output

Privalov Vladimir
1 min readAug 2, 2022

--

In different situations we often need a specific level of verbosity during compilation. Sometimes we need to see less critical warnings, sometimes only critical errors is enough. GCC compiler provides compiler options for outputting different levels of warnings.

You can use compiler options to suppress or adjust level of verbosity during compilation process.

Let’s create a test c++ file.

#include <stdio.h>int main () {
size_t a = 25;
printf("%i\n", a);
int i = 5;
int* b = new int[i];
}

To show all warnings including not important use options -Wall and -Wextra

g++ -Wall -Wextra test.cpp -o test

Output

test.cpp: In function ‘int main()’:
test.cpp:5:19: warning: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘size_t {aka long unsigned int}’ [-Wformat=]
printf("%i\n", a);
^
test.cpp:7:8: warning: unused variable ‘b’ [-Wunused-variable]
int* b = new int[i];

To turn all warnings into errors use option -Werror

g++ -Werror test.cpp -o test

Output:

test.cpp: In function ‘int main()’:
test.cpp:5:19: error: format ‘%i’ expects argument of type ‘int’, but argument 2 has type ‘size_t {aka long unsigned int}’ [-Werror=format=]
printf("%i\n", a);
^
cc1plus: all warnings being treated as errors

That’s it. Enjoy compiling with GCC.

--

--