views:

2348

answers:

5

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?

A: 

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.

1800 INFORMATION
However, #warning isn't a #pragma.
Greg Hewgill
For some reason I read the question as "#pragma warning"
1800 INFORMATION
+2  A: 

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.

Greg Hewgill
A: 

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.]

Jonathan Leffler
+1  A: 

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.

Andrew Edgecombe
+9  A: 

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.

nolandda
This is one of the rare cases, when MS does things in the right way.
doc