sparkandshine sparkandshine - 22 days ago 6
C++ Question

Werror doesn't make all warnings into errors

In general, the flag

-Werror
is to make all warnings into errors. But it is not always the same.

int j;

int main()
{
int i = 10;
return 0;
}


If I type
g++ -Werror main.cpp
, there aren't any tips.

If I type
g++ -Wall main.cpp
, there is a warning:


main.c: In function ‘main’: main.c:5:9: warning: unused variable ‘i’
[-Wunused-variable]


Can anyone explain it?

Moreover,


  1. Why isn't there a warning about the variable
    j
    ?

  2. How can I remove "warnings being treated as errors"?



Thanks.

Answer

-Werror turns all warnings defined by other switches into errors, so you'll have to use -Wall as well as -Werror.

g++ -Werror -Wall main.cpp

Moreover, since j isn't marked static, there is no guarantee that it won't be used outside of the compilation unit it's defined in, so the compiler can't assume it's unused.

Comments