警告不被视为错误 -Wall & -Werror on
warning not treated as error with -Wall & -Werror on
这是源文件的内容 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()
却没有?
如果您查看第一个示例的输出,它说 "error" 在 目标文件 中,这意味着它是由链接器生成的。
第二个错误是由编译器生成的,这意味着没有生成目标文件,因此不会调用链接器。
链接器而非编译器发出gets()
警告,因此编译器设置不适用。
只有链接器能够确定该符号是使用标准库 gets()
解析的,而不是其他同名实现。
要指示链接器将警告视为错误,您需要将其传递给 --fatal-warnings
选项。反过来,当不直接调用链接器时,gcc 使用逗号分隔列表中的 -Wl
选项将选项传递给链接器:
gcc -o get get.c -Wall -Werror -Wl,--fatal-warnings
注意 GNU linker is documented separately from the compiler, as part of binutils. The linker options are described here.
-Werror
将所有警告设为错误,仅打印安全警告,您可以使用:-Wformat -Wformat-security
您可以在此处阅读警告选项 https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
如果发生错误,编译器会中止编译,如果出现一个或多个警告,编译器会继续编译。
这是源文件的内容 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()
却没有?
如果您查看第一个示例的输出,它说 "error" 在 目标文件 中,这意味着它是由链接器生成的。
第二个错误是由编译器生成的,这意味着没有生成目标文件,因此不会调用链接器。
链接器而非编译器发出gets()
警告,因此编译器设置不适用。
只有链接器能够确定该符号是使用标准库 gets()
解析的,而不是其他同名实现。
要指示链接器将警告视为错误,您需要将其传递给 --fatal-warnings
选项。反过来,当不直接调用链接器时,gcc 使用逗号分隔列表中的 -Wl
选项将选项传递给链接器:
gcc -o get get.c -Wall -Werror -Wl,--fatal-warnings
注意 GNU linker is documented separately from the compiler, as part of binutils. The linker options are described here.
-Werror
将所有警告设为错误,仅打印安全警告,您可以使用:-Wformat -Wformat-security
您可以在此处阅读警告选项 https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
如果发生错误,编译器会中止编译,如果出现一个或多个警告,编译器会继续编译。