Portability of #warning preprocessor directive

C++Compiler ConstructionWarningsC PreprocessorPortability

C++ Problem Overview


I know that the #warning directive is not standard C/C++, but several compilers support it, including gcc/g++. But for those that don't support it, will they silently ignore it or will it result in a compile failure? In other words, can I safely use it in my project without breaking the build for compilers that don't support it?

C++ Solutions


Solution 1 - C++

It should be noted that MSVC uses the syntax:

#pragma message ( "your warning text here" )

The usual #warning syntax generates a fatal error

C1021: invalid preprocessor command 'warning'

so it is not portable to those compilers.

Solution 2 - C++

It is likely that if a compiler doesn't support #warning, then it will issue an error. Unlike #pragma, there is no recommendation that the preprocessor ignore directives it doesn't understand.

Having said that, I've used compilers on various different (reasonably common) platforms and they have all supported #warning.

Solution 3 - C++

You are likely to get at least an unrecognized directive warning from compilers that don't recognize #warning, even if the code block is not included in your compilation. That might or might not be treated as an error - the compiler could legitimately treat it as an error, but many would be more lax.

Are you aware of (can you name) a compiler other than GCC/G++ that provides #warning? [Edited: Sun Solaris 10 (Sparc) and the Studio 11 C/C++ compilers both accept #warning.]

Solution 4 - C++

I had this problem once with a compiler for an Atmel processor. And it did generate preprocessor errors due to the unknown #warning token.

Unfortunately the solution seemed to be to convert the whole source tree to use the #pragma equivalent and accept that the build behavior was going to differ if using gcc.

Solution 5 - C++

When switching from mingw to visual studio, I added such lines to my global config header. (include it in stdafx.h)

#ifdef __GNUC__
//from https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
//Instead of put such pragma in code:
//#pragma GCC diagnostic ignored "-Wformat"
//use:
//PRAGMA_GCC(diagnostic ignored "-Wformat")
#define DO_PRAGMA(x) _Pragma (#x)
#define PRAGMA_GCC(x) DO_PRAGMA(GCC #x)

#define PRAGMA_MESSAGE(x) DO_PRAGMA(message #x)
#define PRAGMA_WARNING(x) DO_PRAGMA(warning #x)
#endif //__GNUC__
#ifdef _MSC_VER
/*
#define PRAGMA_OPTIMIZE_OFF __pragma(optimize("", off))
// These two lines are equivalent
#pragma optimize("", off)
PRAGMA_OPTIMIZE_OFF
*/
#define PRAGMA_GCC(x)
// https://support2.microsoft.com/kb/155196?wa=wsignin1.0
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __PRAGMA_LOC__ __FILE__ "("__STR1__(__LINE__)") "
#define PRAGMA_WARNING(x) __pragma(message(__PRAGMA_LOC__ ": warning: " #x))
#define PRAGMA_MESSAGE(x) __pragma(message(__PRAGMA_LOC__ ": message : " #x))

#endif

//#pragma message "message quoted"
//#pragma message message unquoted

//#warning warning unquoted
//#warning "warning quoted"

PRAGMA_MESSAGE(PRAGMA_MESSAGE unquoted)
PRAGMA_MESSAGE("PRAGMA_MESSAGE quoted")

#warning "#pragma warning quoted"
    
PRAGMA_WARNING(PRAGMA_WARNING unquoted)
PRAGMA_WARNING("PRAGMA_WARNING quoted")

Now I use PRAGMA_WARNING(this need to be fixed)

Sadly there is no #pragma warning in gcc, so it warns unspecified pragma.

I doubt that gcc will add #pragma warning" rather than microsoft adding #warning.

Solution 6 - C++

Actually most compilers that I know about ignore unknown #pragma directives, and output a warning message - so in the worst case, you'll still get a warning.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjonnerView Question on Stackoverflow
Solution 1 - C++nolanddaView Answer on Stackoverflow
Solution 2 - C++Greg HewgillView Answer on Stackoverflow
Solution 3 - C++Jonathan LefflerView Answer on Stackoverflow
Solution 4 - C++Andrew EdgecombeView Answer on Stackoverflow
Solution 5 - C++FantastoryView Answer on Stackoverflow
Solution 6 - C++1800 INFORMATIONView Answer on Stackoverflow