简体   繁体   English

抑制多个编译器构建的警告

[英]suppress warnings on multiple compiler builds

Is there a generic suppress warning that i can use? 是否有可以使用的通用抑制警告?

The problem is that there are times i may build using one compiler version (gcc) and then i have a partner that uses some of the common things but uses a different compiler. 问题是有时我可能使用一个编译器版本(gcc)构建,然后我有一个使用一些常见事物但使用不同编译器的伙伴。 So the warning # are different. 所以警告#是不同的。

The only way i could think of doing was making a macro that was defined in a file that i would pass in some generic value: 我能想到的唯一方法是创建一个在文件中定义的宏,我将传递一些通用值:

SUPPRESS_WARNING_BEGIN(NEVER_USED)
//code
SUPPRESS_WARNING_END

then the file would have something like: 那么文件会有类似的东西:

#if COMPILER_A
    NEVER_USED = 245
#endif

#if COMPILER_B
    NEVER_USED = 332
#endif


#define SUPPRESS_WARNING_BEGIN(x) /
     #if COMPILER_A
        //Compiler A suppress warning x
     #endif

     #if COMPILER_B
        //Compiler B suppress warning x
     #endif

#define SUPPRESS_WARNING_END /
     #if COMPILER_A
        // END Compiler A suppress warning
     #endif

     #if COMPILER_B
        // END Compiler A suppress warning
     #endif

Don't know if there is an easier way? 不知道有没有更简单的方法? Also i know ideally we all would just use the same compiler but that is unfortunately not an option. 另外我知道理想情况下我们都会使用相同的编译器,但遗憾的是这不是一个选项。 Just trying to find the least complicated way to support something like this and am hoping there is a simpler way then mentioned above. 只是试图找到最简单的方式来支持这样的事情,并希望有一个更简单的方法,然后在上面提到。

thanks 谢谢

There's no portable way to do that. 没有可移植的方法来做到这一点。 Different compilers do it in different ways (eg #pragma warning , #pragma GCC diagnostic , etc.). 不同的编译器以不同的方式执行它(例如#pragma warning#pragma GCC diagnostic等)。

The easiest and best thing to do is to write code that does not generate any warnings with at compiler at any warning level. 最简单和最好的做法是编写在任何警告级别都不会在编译器中生成任何警告的代码。

If your goal is to suppress warnings about unused variables, I recommend using a macro: 如果你的目标是禁止有关未使用变量的警告,我建议使用宏:

#define UNUSED(x) ((void)sizeof(x))
...
void some_function(int x, int y)
{
    // No warnings will be generated if x is otherwise unused
    UNUSED(x);
    ....
}

The sizeof operator is evaluated at compile-time, and the cast to void produces no result, so any compiler will optimize the UNUSED statement away into nothing but consider the operand to be used. sizeof运算符在编译时进行求值,而转换为void不会产生任何结果,因此任何编译器都会将UNUSED语句优化UNUSED ,只考虑要使用的操作数。

GCC also has the unused attribute` : GCC还有unused属性`

// No warnings will be generated if x is otherwise unused
int x __attribute__((unused));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM