Yua*_*Wen 4 c gcc compiler-errors compiler-warnings
这是源文件get.c的内容:
#include <stdio.h>
int main(){
//int i = 0;
char b[10];
gets(b);
puts(b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我用这些命令编译它时
Run Code Online (Sandbox Code Playgroud)gcc -o get get.c -Wall -Werror
输出是
Run Code Online (Sandbox Code Playgroud)/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;
}
Run Code Online (Sandbox Code Playgroud)
使用相同的命令,输出是
Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
请注意,GNU链接器与编译器分开记录,作为binutils的一部分.此处描述了链接器选项.
归档时间: |
|
查看次数: |
1477 次 |
最近记录: |