sparkandshine sparkandshine - 1 year ago 88
C++ Question

Werror doesn't make all warnings into errors

In general, the flag

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’

Can anyone explain it?


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

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


Answer Source

-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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download