选择性地仅对翻译单元的一部分禁用 GCC 警告

2021-12-18 00:00:00 gcc c c++ compiler-warnings pragma

与此 MSVC 预处理器代码最接近的 GCC 是什么?>

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 for. However, we want files which include those headers to continue to generate that warning (if the project has that warning enabled).

推荐答案

这是 自 4.6 版起可在 GCC 中,或在 2010 年 6 月左右在主干中.

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

这是一个例子:

#pragma GCC diagnostic push
#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 */

相关文章