Gcc 如何在使用-Werror时忽略错误?

Gcc 如何在使用-Werror时忽略错误?,gcc,gcc-warning,Gcc,Gcc Warning,我有以下测试代码test.c: #include<stdio.h> int *func() { int i = 123; return &i; } int main() { printf("%d\n", *func()); } 它将具有以下警告信息: warning: address of stack memory associated with local variable 'i' returned [-Wreturn-stack-addre

我有以下测试代码
test.c

#include<stdio.h>
int *func()
{
    int i = 123;
    return &i;
}

int main()
{
    printf("%d\n", *func());
}
它将具有以下警告信息:

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
选项,但我还想忽略与局部变量“I”相关联的堆栈内存的
地址
警告,我该怎么办?

大多数
gcc
警告可以通过在警告名称前加上
否-
,例如
-Wno返回堆栈地址


也就是说,这不是你想忽视的事情;返回指向堆栈变量的指针是一种未定义的行为,虽然在大多数编译器上它的结果是半可预测的,但它非常脆弱;任何函数调用,无论是隐式的还是显式的,都可能会对指针引用的值产生影响。

您意识到将指针返回堆栈变量是一个糟糕的想法,对吗?如果您进行任何函数调用(包括隐式函数调用,有时在使用
={0};
初始化结构/数组时执行),指针引用的值将不再有效。这是一个有原因的警告。谢谢@ShadowRanger我得到了它,我只想暂时解决它。
error: address of stack memory associated with local variable 'i'
  returned [-Werror,-Wreturn-stack-address]
return &i;
        ^
1 error generated.