Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 354
Thomas Avatar asked Jul 02 '10 02:07

Thomas


People also ask

What are compiler warnings?

Compiler warnings are messages produced by a compiler regarding program code fragments to be considered by the developer, as they may contain errors. Unlike compilation errors, warnings don't interrupt the compilation process.

Which option can be used to display compiler warnings?

You can use a #pragma warning directive to control the level of warning that's reported at compile time in specific source files. Warning pragma directives in source code are unaffected by the /w option.


2 Answers

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.

like image 164
Thomas Avatar answered Nov 15 '22 15:11

Thomas


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.

like image 41
Inverse Avatar answered Nov 15 '22 15:11

Inverse