#warning in Visual Studio - c++

In gcc I can do compile-time warnings like this:
#if !defined(_SOME_FEATURE_)
#warning _SOME_FEATURE_ not defined-- be careful!
#endif
But in Visual Studio this doesn't work. Is there an alternative syntax for #warning?

About the closest equivalent would be #pragma message, or possibly #error (the latter stops compilation, the former just prints out the specified error message).

Use #pragma message("Some message")

There is a good article here on how to achieve a similar effect to #warning in visual studio:
http://goodliffe.blogspot.co.uk/2009/07/c-how-to-say-warning-to-visual-studio-c.html
Edit: Here is the relevant section from the above link, however I do recommend reading the article.
#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define WARNING(desc) message(__FILE__ "(" STRINGIZE(__LINE__) ") : Warning: " #desc)
// usage:
#pragma WARNING(FIXME: Code removed because...)

#pragma WEIRD_VALUES_HERE
is the way I've always seen it done. M$ probably has the pragmas on their site somewhere.

Another thought is template style compile time asserts. Boost has a whole selection of these if you are wanting to check compile time code errors.

Related

c++ pragma #error giving an error

I recently came across a piece of code which uses
the pragma directive
#error Error ! Define [_HOS_ | _HOS_OV_ | _HOV_].
This code is from the ADOC-C jacobian computation routine.
The problem is that in C++ visual studio 2010 there is a curly red line below the #error indicating there is some problem within that line.
The code also fails to compile giving error message at that line
Is there a syntax issue or are those [_HOS_ | _HOS_OV_ | _HOV_] not defined at that point, and intelliSense knows that resulting in curly red line indicating some problem?
The author of that code intended to have an error when not at least one of the tags named in the error message is defined. You should have a look at the documentation of the ADOC-C stuff if s.th. is mentioned there about these tags.
The intent of the #error directive is to create compile errors. It's usually a way for the programmer who wrote the code to tell the programmer that's trying to compile and use it "You did something wrong, this won't work!". The string following the directive is the message that should be shown to the programmer trying to compile the code.
You should check what directives are around this one, for example are there any #ifdefs that cause it to be executed. Then you should lookup the conditions in which they executed (e.g. not defining the things the error lists) and look for a way to make them go away.
The problem is that in C++ visual studio 2010 there is a curly red line below the #error indicating there is some problem within that line.
Visual Studio 2010 does not recognize #error preprocesor syntax anymore. It only recognize #pragma warning

Intentional compiler warnings for Visual C++ that appear in Error List?

How can you create a compiler warning (in the model of #error, except as a warning) on purpose in Visual C++ that will show up on the Error List with the correct file and line number?
GCC and other compilers offer #warning, but the MSVC compiler does not.
The "solution" at http://support.microsoft.com/kb/155196 does not parse in the Visual Studio error list.
Just add this to your common include file (ex, stdafx.h):
#define __STR2__(x) #x
#define __STR1__(x) __STR2__(x)
#define __LOC__ __FILE__ "("__STR1__(__LINE__)") : warning W0000: #pragma VSWARNING: "
#define VSWARNING(x) message(__LOC__ x)
Use this like:
#pragma VSWARNING("Is this correct?!?!")
The compiler will output:
c:\dir\file.h(11) : warning W0000: #pragma VSWARNING: Is this correct?!?!
And the Error List tab will show the warning nicely in the table:
Type Num Description File Line
[Warning] 13 warning W0000: #pragma VSWARNING: Is this correct?!?! file.h 11
exactly like a normal Visual Studio compiler warning.
This is kind of a silly answer to your question, but often, if I need to add an intentional warning, I will type something like:
#pragma asdfkljasdlfjasklfjklasjdfklj
which issues a Unknown Pragma warning with line number and all.

#warning and macro evaluation

I have the following code :
#define LIMIT_DATE \"01-03-2010\"
#ifdef LIMIT_DATE
#if _MSC_VER
#pragma message ("Warning : this release will expire on " LIMIT_DATE)
#elif __GNUC__
#warning ("Warning : this release will expire on " LIMIT_DATE)
#endif
#endif
The problem is that LIMIT_DATE is not evaluated when printing the warning.
I searched on Google, but didn't found yet the solution.
Thanks for help.
From gcc preprocessor documentation
Neither #error nor #warning
macro-expands its argument. Internal
whitespace sequences are each replaced
with a single space. The line must
consist of complete tokens. It is
wisest to make the argument of these
directives be a single string
constant; this avoids problems with
apostrophes and the like.
So it's not possible at least in gcc.
According to MSDN this should work for MSVC althrough I don't have access to Visual Studio currently to test this

C++ Disabling warnings on a specific include

I'm wondering if there's a way to disable all warnings on a specific file (for example, using a preprocessor directive).
I'm using CImg.h and I want to get rid of the warnings involving that code.
I'm compiling both with VS (the version for Windows) and gcc (the Linux one), so I would like to have a generic way...
Thanks!
You can do it using #pragma in Microsoft compiler:
http://msdn.microsoft.com/en-us/library/2c8f766e%28VS.80%29.aspx
Something like this:
#pragma warning (push, 0)
//....header file
#pragma warning (pop)
Can't help you with gcc compiler, some info here: Selectively disable GCC warnings for only part of a translation unit?
EDIT EDIT Try push, 0.
Look into #pragma warning.

Portability of #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 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?
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.
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.
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.]
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.
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.
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.