Portability of the #warning Preprocessor Directive

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

+47
c ++ compiler-construction c-preprocessor portability warnings
Oct 05 '08 at 3:40
source share
6 answers

It is likely that if the compiler does not support #warning, it will throw an error. Unlike #pragma, there is no recommendation that the preprocessor ignore directives that it does not understand.

Having said that, I used compilers on different (fairly common) platforms, and they all supported #warning.

+27
05 Oct '08 at 3:53 a.m.
source share

It should be noted that MSVC uses the syntax:

 #pragma message ("your warning text here")

Regular syntax # warning generates fatal error

 C1021: invalid preprocessor command 'warning'

therefore, it is not portable for these compilers.

+68
Nov 11 '08 at 0:30
source share

You will probably receive at least an unrecognized directive warning from compilers that do not recognize #warning, even if the code block is not included in your compilation. This may or may not be perceived as an error - the compiler may legitimately regard it as an error, but many of them will be weaker.

Do you know (can you name) a compiler other than GCC / g ++ that #warning provides? [Edited: Sun Solaris 10 (Sparc) and Studio 11 C / C ++ compilers accept #warning.]

+3
Oct 05 '08 at 3:56
source share

I had this problem once with a compiler for an Atmel processor. And that caused preprocessor errors due to an unknown #warning token.

Unfortunately, the solution seemed to be to convert the entire source tree to the #pragma equivalent and accept that the assembly behavior would be different when using gcc.

+1
Oct 05 '08 at 4:20
source share

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

0
05 Oct '08 at 3:48
source share

When switching from mingw to visual studio, I added these lines to my global configuration 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 am using PRAGMA_WARNING (this needs to be fixed)

Unfortunately, gcc does not have #pragma warning , so it warns an unspecified pragma.

I doubt that gcc will add #pragma warning" instead of adding Microsoft #warning .

0
Oct 20 '16 at 7:24
source share



All Articles