使用 pragma 抑制 gcc 4.2.1 警告
我想抑制 gcc 由于返回局部变量的地址而发出的特定警告。
#include <stdio.h>
#pragma GCC diagnostic ignored "-Waddress"
void *get_stack() {
unsigned long v;
return &v;
}
int main()
{
void *p = get_stack();
printf("stack is %p\n",p);
return 0;
}
>gcc -fdiagnostics-show-option p.c
p.c: In function ‘get_stack’:
p.c:5: warning: function returns address of local variable
平台:此问题至少存在于 MacOSX 10.5 Snow Leopard 上, 我还没有在 Linux 上尝试过。
如果您想知道为什么:我想在警告变成错误的情况下运行 停止冗长的构建过程,这样我就可以真正看到问题并被迫 修复它们。
这个特定的代码不是一个错误,它是一种“便携式”技术,用于查找 堆栈指针(也适用于 MSVC)。 [实际上它不会工作 Itanium 有两个堆栈指针]
垃圾收集例程需要使用堆栈指针 (在挂起线程的堆栈上搜索指针)。
I would like to suppress a particular warning issued by gcc caused by returning the address of a local variable.
#include <stdio.h>
#pragma GCC diagnostic ignored "-Waddress"
void *get_stack() {
unsigned long v;
return &v;
}
int main()
{
void *p = get_stack();
printf("stack is %p\n",p);
return 0;
}
>gcc -fdiagnostics-show-option p.c
p.c: In function ‘get_stack’:
p.c:5: warning: function returns address of local variable
Platform: this issue exists at least on MacOSX 10.5 Snow Leopard,
I haven't tried on Linux yet.
In case you're wondering why: I would like to run with warnings turned into errors
to halt a long winded build process so I can actually SEE problems and be forced
to fix them.
This particular code isn't a bug, it is a "portable" technique for finding a
the stack pointer (which works on MSVC too). [Actually it won't work on the
Itanium which has two stack pointers]
The stack pointer is required for use by a garbage collection routine
(to search for pointers on the stacks of suspended threads).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这似乎使警告对我来说消失了:
This appears to make the warning go away for me:
正如 docs 注释,您只能控制显示的选项 <代码>-fdiagnostics-show-option。它没有显示给我。我正在运行 4.4.1,但我怀疑 4.2.1 是否也会如此。
您可能需要提交错误以将其包含在诊断系统中。
As the docs note, you can only control options that show up for
-fdiagnostics-show-option
. It does not show up for me. I'm running 4.4.1, but I doubt it would for 4.2.1 either.You may want to file a bug to get it included in the diagnostic system.