Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to selectively ignore a GCC warning

📅 2014-Mar-11 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ gcc, pragma, warning ⬩ 📚 Archive

You may sometimes want to ignore or disable a specific type of warning that is generated when compiling your C or C++ code. The diagnostic pragmas of GCC and G++ compilers can be used to achieve this.

#pragma GCC diagnostic ignored "-Wenum-compare"
/**
 * Code that generates this warning
 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wenum-compare"
/**
 * Code that generates this warning
 */
#pragma GCC diagnostic pop

Note: As of GCC 5.1, few of the warnings in G++ (that is, only for C++ code) will not be ignored even when this pragma is used! More details here and here.

Tried with: GCC 4.8 and Ubuntu 12.04 LTS


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧