How to disable #pragma warnings?

醉酒当歌 提交于 2019-11-28 07:08:43

I believe you can compile with

-Wno-unknown-pragmas

to suppress these.

In GCC, compile with -Wno-unknown-pragmas

In MS Visual Studio 2005 (this question isn't tagged with gcc, so I'm adding this for reference), you can disable globally in Project Settings->C/C++->Advanced. Enter 4068 in "Disable Specific Warnings"

or you can add this to any file to disable warnings locally

#pragma warning (disable : 4068 ) /* disable unknown pragma warnings */

Perhaps see GCC Diagnostic Pragmas? Alternatively in this case you could use the combination of options that -Wall enables, excluding -Wunknown-pragmas.

Thank you every one for the tip. In my case, I work with Qt Mingw. I need to set flag another way, in my .PRO file:

QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas

I know the question is about GCC, but for people wanting to do this as portably as possible:

Most compilers which can emit this warning have a way to disable the warning from either the command line (exception: PGI) or in code (exception: DMC):

  • GCC: -Wno-unknown-pragmas / #pragma GCC diagnostic ignored "-Wunknown-pragmas"
  • clang: -Wno-unknown-pragmas / #pragma clang diagnostic ignored "-Wunknown-pragmas"
  • Intel C/C++ Compiler: -diag-disable 161 / #pragma warning(disable:161)
  • PGI: #pragma diag_suppress 1675
  • MSVC: -wd4068 / #pragma warning(disable:4068)
  • TI: --diag_suppress,-pds=163 / #pragma diag_suppress 163
  • IAR C/C++ Compiler: --diag_suppress Pe161 / #pragma diag_suppress=Pe161
  • Digital Mars C/C++ Compiler: -w17

You can combine most of this into a single macro to use in your code, which is what I did for the HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS macro in Hedley:

#if HEDLEY_HAS_WARNING("-Wunknown-pragmas")
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"")
#elif HEDLEY_INTEL_VERSION_CHECK(16,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)")
#elif HEDLEY_PGI_VERSION_CHECK(17,10,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675")
#elif HEDLEY_GNUC_VERSION_CHECK(4,3,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"")
#elif HEDLEY_MSVC_VERSION_CHECK(15,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068))
#elif HEDLEY_TI_VERSION_CHECK(8,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163")
#elif HEDLEY_IAR_VERSION_CHECK(8,0,0)
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161")
#else
#  define HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS
#endif

Note that Hedley may have more complete information than this answer since I'll probably forget to update this answer, so if you don't want to just use Hedley (it's a single public domain C/C++ header you can easily drop into you project) you might want to use Hedley as a guide instead of the information above.

The version checks are probably overly pessimistic, but sometimes it's hard to get good info about obsolete versions of proprietary compilers, and I'd rather be safe than sorry. Again, Hedley's information may be better.

Many compilers can also push/pop warnings onto a stack, so you can push, then disable them before including code you don't control, then pop so your code will still trigger the warning in question (so you can clean it up). There are macros for that in Hedley, too: HEDLEY_DIAGNOSTIC_PUSH / HEDLEY_DIAGNOSTIC_POP.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!