这是源文件 get.c 的内容:
#include <stdio.h>
int main(){
//int i = 0;
char b[10];
gets(b);
puts(b);
return 0;
}
当我用这些命令编译它时
gcc -o get get.c -Wall -Werror
输出是
/tmp/ccYEWZvx.o: In function `main': get.c:(.text+0x10): warning: the `gets' function is dangerous and should not be used.
但是当把代码改成
#include <stdio.h>
int main(){
int i = 0; // **this line just be uncommented**
char b[10];
gets(b);
puts(b);
return 0;
}
使用同样的命令,输出为
cc1: warnings being treated as errors get.c: In function 'main': get.c:4: error: unused variable 'i'
那么,为什么这个未使用的变量警告被视为错误,而 gets()
的使用却没有?
最佳答案
gets()
警告是由链接器而非编译器发出的,因此编译器设置不适用。
只有链接器能够确定该符号是使用标准库 gets()
解析的,而不是使用相同名称的其他实现。
要指示链接器将警告视为错误,您需要将 --fatal-warnings
选项传递给它。反过来,当不直接调用链接器时,gcc 使用逗号分隔列表中的 -Wl
选项将选项传递给链接器:
gcc -o get get.c -Wall -Werror -Wl,--fatal-warnings
请注意 GNU linker与编译器分开记录,作为 binutils 的一部分.描述了链接器选项 here .
关于c - 警告不被视为错误 -Wall & -Werror on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753760/