使用-Werror时如何忽略错误?
How ignore error when use -Werror?
我有以下测试代码test.c
:
#include<stdio.h>
int *func()
{
int i = 123;
return &i;
}
int main()
{
printf("%d\n", *func());
}
如果我用命令编译就OK:
gcc test.c -o test
它将有以下警告信息:
warning: address of stack memory associated with local variable 'i'
returned [-Wreturn-stack-address]
return &i;
^
1 warning generated.
但是可以输出结果:123
如果我使用命令:
gcc -Werror test.c -o test
它将有以下错误信息:
error: address of stack memory associated with local variable 'i'
returned [-Werror,-Wreturn-stack-address]
return &i;
^
1 error generated.
现在我想使用-Werror
选项,但我又想忽略address of stack memory associated with local variable 'i'
警告,我该怎么办?
大多数 gcc
警告可以通过在警告名称前加上前缀 no-
来禁用,例如-Wno-return-stack-address
.
也就是说,这不是您想要忽略的事情;返回指向堆栈变量的指针是未定义的行为,虽然它在大多数编译器上具有半可预测的结果,但它非常脆弱;任何函数调用,无论是隐式的还是显式的,都可能会破坏指针所引用的值。
我有以下测试代码test.c
:
#include<stdio.h>
int *func()
{
int i = 123;
return &i;
}
int main()
{
printf("%d\n", *func());
}
如果我用命令编译就OK:
gcc test.c -o test
它将有以下警告信息:
warning: address of stack memory associated with local variable 'i'
returned [-Wreturn-stack-address]
return &i;
^
1 warning generated.
但是可以输出结果:123
如果我使用命令:
gcc -Werror test.c -o test
它将有以下错误信息:
error: address of stack memory associated with local variable 'i'
returned [-Werror,-Wreturn-stack-address]
return &i;
^
1 error generated.
现在我想使用-Werror
选项,但我又想忽略address of stack memory associated with local variable 'i'
警告,我该怎么办?
大多数 gcc
警告可以通过在警告名称前加上前缀 no-
来禁用,例如-Wno-return-stack-address
.
也就是说,这不是您想要忽略的事情;返回指向堆栈变量的指针是未定义的行为,虽然它在大多数编译器上具有半可预测的结果,但它非常脆弱;任何函数调用,无论是隐式的还是显式的,都可能会破坏指针所引用的值。