Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top