简体   繁体   中英

How to suppress the “enumeral and non-enumeral type in conditional expression” warning in GCC

I keep getting this warning from a third-party library (which I don't want to debug), so I'd really appreciate a way to suppress this specific warning. Google failed me, so here I am.

In gcc4.6 and later you can use pragma's to suppress specific warnings and do that suppression only to a specific block of code, ie :

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wno-enum-compare" 
// Code that causes warning goes here
#pragma GCC diagnostic pop

The push/pop are used to preserve the diagnostic options that were in place before your code was processed.

This would be a much better approach than using #pragma GCC system_header to suppress all warnings. (Of course, in older gcc you may be "stuck" with the #pragma GCC system_header approach!)

Here's a nice reference on suppressing gcc warnings: http://www.dbp-consulting.com/tutorials/SuppressingGCCWarnings.html

This page also describes how to use -fdiagnostics-show-option to find out what option controls a particular warning.

Of course, it's generally far preferable to fix the root cause of all warnings than to suppress them! However, sometimes that is not possible.

-Wno-enum-compare bypasses this warning.

See also

以下标志不会摆脱那个警告吗?

-Wno-enum-promotion

Well, since I couldn't find a way to disable this specific warning, I resorted to using gcc's #pragma system_header. Basically, I wrapped the problematic header like this:

#if defined __GNUC__
#pragma GCC system_header
#elif defined __SUNPRO_CC
#pragma disable_warn
#elif defined _MSC_VER
#pragma warning(push, 1)
#endif

#include "foo.h"

#if defined __SUNPRO_CC
#pragma enable_warn
#elif defined _MSC_VER
#pragma warning(pop)
#endif

where foo.h was the problematic header. Now I just include this fooWrapper.h and the problem goes away. Note that this should work for some other compilers too (MSC and SUNPRO), but I didn't test it.

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