出现在错误列表中的Visual C++的故意编译器警告?

Tho*_*mas 8 c++ compiler-construction pragma compiler-warnings visual-studio

如何在Visual C++中故意创建一个编译器警告(在#error模型中,除了作为警告),它将在错误列表中显示正确的文件和行号?

GCC和其他编译器提供#warning,但MSVC编译器没有.

http://support.microsoft.com/kb/155196上的"解决方案" 未在Visual Studio错误列表中进行解析.

Tho*_*mas 11

只需将其添加到您的公共包含文件(例如,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)
Run Code Online (Sandbox Code Playgroud)

使用它像:

#pragma VSWARNING("Is this correct?!?!")
Run Code Online (Sandbox Code Playgroud)

编译器将输出:

c:\dir\file.h(11) : warning W0000: #pragma VSWARNING: Is this correct?!?!
Run Code Online (Sandbox Code Playgroud)

错误列表选项卡将在表中很好地显示警告:

Type       Num   Description                                             File    Line
[Warning]  13    warning W0000: #pragma VSWARNING: Is this correct?!?!   file.h  11
Run Code Online (Sandbox Code Playgroud)

与正常的Visual Studio编译器警告完全一样.