仅针对部分翻译单元选择性禁用GCC警告? [英] Selectively disable GCC warnings for only part of a translation unit?

查看:196
本文介绍了仅针对部分翻译单元选择性禁用GCC警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #pragma warning(push)//保存当前的警告州。 
#pragma警告(禁用:4723)// C4723:潜在的除以0
//代码会产生警告4723.
#pragma警告(弹出)//将警告恢复到之前的状态。

我们在通常包含的头文件中有代码,我们不希望产生特定的警告。但是,我们希望包含这些头文件的文件能够继续生成该警告(如果项目启用了该警告)。

从4.6版开始可以在在GCC中使用,或者在2010年6月前后。

下面是一个例子:

  #pragma GCC诊断错误 -Wuninitialized
foo(a); / *给这个错误* /
#pragma GCC诊断推送
#pragma忽略GCC诊断-Wuninitialized
foo(b); / *无此诊断* /
#pragma GCC诊断弹出
foo(c); / *此错误给出* /
#pragma GCC诊断流行
foo(d); / *取决于命令行选项* /


What's the closest GCC equivalent to this MSVC preprocessor code?

#pragma warning( push )                    // Save the current warning state.
#pragma warning( disable : 4723 )          // C4723: potential divide by 0
// Code which would generate warning 4723.
#pragma warning( pop )                     // Restore warnings to previous state.

We have code in commonly included headers which we do not want to generate a specific warning. However we want files which include those headers to continue to generate that warning (if the project has that warning enabled).

解决方案

This is possible in GCC since version 4.6, or around June 2010 in the trunk.

Here's an example:

#pragma GCC diagnostic error "-Wuninitialized"
    foo(a);         /* error is given for this one */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wuninitialized"
    foo(b);         /* no diagnostic for this one */
#pragma GCC diagnostic pop
    foo(c);         /* error is given for this one */
#pragma GCC diagnostic pop
    foo(d);         /* depends on command line options */

这篇关于仅针对部分翻译单元选择性禁用GCC警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆