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
.