Marcos Bento
Marcos Bento

Reputation: 2040

How can I disable #pragma warnings?

While developing a C++ application, I had to use a third-party library which produced a huge amount of warnings related with a harmless #pragma directive being used.

../File.hpp:1: warning: ignoring #pragma ident
In file included from ../File2.hpp:47,
                 from ../File3.hpp:57,
                 from File4.h:49,

Is it possible to disable this kind of warnings, when using the GNU C++ compiler?

Upvotes: 54

Views: 87958

Answers (5)

nemequ
nemequ

Reputation: 17472

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
  • Cray: -h nomessage=1234

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.

Upvotes: 22

pamplemousse_mk2
pamplemousse_mk2

Reputation: 187

In my case, I work with Qt under MinGW. I need to set the flag another way, in my .PRO file:

QMAKE_CXXFLAGS_WARN_ON += -Wno-unknown-pragmas

Upvotes: 8

Adam Wright
Adam Wright

Reputation: 49376

I believe you can compile with

-Wno-unknown-pragmas

to suppress these.

Upvotes: 108

Airsource Ltd
Airsource Ltd

Reputation: 32622

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 */

Upvotes: 31

Mike Dimmick
Mike Dimmick

Reputation: 9802

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

Upvotes: 18

Related Questions