简体   繁体   中英

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. 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.).

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.

GCC also has the unused attribute` :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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