8

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.

Thomas
  • 233
  • 2
  • 11
  • looks like you are confused about what exactly is Stack Overflow. – Juliano Jul 02 '10 at 03:05
  • It's an S.O. sort of question. I also would like to know what others would do better. – Thomas Jul 02 '10 at 03:07
  • @Juliano: What exactly do you find objectionable about this question? It's most certainly on-topic and interesting. It might be a duplicate, but I couldn't find a duplicate question. – James McNellis Jul 02 '10 at 03:10
  • @James: He posted the question *and* the answer. It looked like a blog post or something. Now he edited it, but it is still strange. Take a look at the revision history to understand. – Juliano Jul 02 '10 at 03:12
  • 2
    @Juliano: The FAQ specifically says that this kind of question is just fine (even question/answer type) and has said so since the beta. – Jason Coco Jul 02 '10 at 03:16
  • @Jason do what I suggested and look at the revision history of the question. He posted the answer in the question area, which was clearly wrong. Now he edited the question. Geez... – Juliano Jul 02 '10 at 03:19

2 Answers2

11

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.

Thomas
  • 233
  • 2
  • 11
  • 1
    I use a similar implementation to this (I posted it [in response to another question](http://stackoverflow.com/questions/2703528/what-code-have-you-written-with-pragma-you-found-useful/2706693#2706693)). I didn't realize that Visual C++ would macro replace the text of a pragma directive; that's very cool. – James McNellis Jul 02 '10 at 03:08
  • 1
    On a related note, you can also use "error" instead of "warning" in the output text; this causes compilation to fail (like `#error` does), but allows compilation to continue to the end of the translation unit (which is extraordinarily useful in some scenarios). – James McNellis Jul 02 '10 at 03:13
1

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.

Inverse
  • 4,408
  • 2
  • 26
  • 35
  • That doesn't print the actual message in the compiler output and Error List, though. Only "unknown pragma" appears. – Thomas Jul 02 '10 at 04:21