Werror没有将所有警告都变成错误

Spa*_*ine 2 c++ gcc g++

通常,该标志-Werror是使所有警告成为错误.但它并不总是一样的.

int j;

int main()
{
    int i = 10; 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我输入g++ -Werror main.cpp,则没有任何提示.
如果我输入g++ -Wall main.cpp,则会出现警告:

main.c:在函数'main'中:main.c:5:9:警告:未使用的变量'i'[-Wunused-variable]

有人能解释一下吗?

此外,

  1. 为什么没有关于变量的警告j
  2. 如何删除"警告被视为错误"?

谢谢.

ant*_*ijn 7

-Werror将其他开关定义的所有警告变为错误,因此您必须使用-Wall以及-Werror.

g++ -Werror -Wall main.cpp
Run Code Online (Sandbox Code Playgroud)

此外,由于j未标记static,因此无法保证它不会在其定义的编译单元之外使用,因此编译器不能认为它未使用.