开发者

force warning/error

开发者 https://www.devze.com 2023-03-15 02:05 出处:网络
I want to put some warning or error into 开发者_运维知识库my code. I am using visual studio 2010.

I want to put some warning or error into 开发者_运维知识库my code. I am using visual studio 2010.

I used #error and #warning in Xcode, but visual studio doesn't know those directives.


After some searching through different articles, I finally came to this solution which works in Visual Studio 2010:

#define STRINGIZE_HELPER(x) #x
#define STRINGIZE(x) STRINGIZE_HELPER(x)
#define __MESSAGE(text) __pragma( message(__FILE__ "(" STRINGIZE(__LINE__) ")" text) ) 
#define WARNING(text) __MESSAGE( " : Warning: " #text )
#define ERROR(text) __MESSAGE( " : Error: " #text )
#define MESSAGE(text) __MESSAGE( ": " #text )
#define TODO(text) WARNING( TODO: text )

and you can use it as:

WARNING( This will be a compiler warning );
ERROR( This will be a compiler error );
MESSAGE( Well this is what I have to say about this code );
TODO( Still have to fix 3D rendering );

Note that TODO() will also generate a compiler warning; if you don't want to register your TODOs as warnings just use this instead:

#define TODO(text) MESSAGE( TODO: text )

If you want to display function name inside warnings/errors/TODOs, use this instead:

#define WARNING(text) __MESSAGE( " : Warning: (" __FUNCTION__ "): " #text )
#define ERROR(text) __MESSAGE( " : Error: (" __FUNCTION__ "): " #text )
#define MESSAGE(text) __MESSAGE( ": (" __FUNCTION__ "): " #text )
#define TODO(text) __MESSAGE( " : Warning: TODO: (" __FUNCTION__ ") " #text )


This suggestion is a bit late I know, but...

You can achieve what you want with the following trick:

// stringised version of line number (must be done in two steps)
#define STRINGISE(N) #N
#define EXPAND_THEN_STRINGISE(N) STRINGISE(N)
#define __LINE_STR__ EXPAND_THEN_STRINGISE(__LINE__)

// MSVC-suitable routines for formatting <#pragma message>
#define __LOC__ __FILE__ "(" __LINE_STR__ ")"
#define __OUTPUT_FORMAT__(type) __LOC__ " : " type " : "

// specific message types for <#pragma message>
#define __WARN__ __OUTPUT_FORMAT__("warning")
#define __ERR__ __OUTPUT_FORMAT__("error")
#define __MSG__ __OUTPUT_FORMAT__("programmer's message")
#define __TODO__ __OUTPUT_FORMAT__("to do")

Then to generate a message, do e.g.:

#pragma message ( __MSG__ "my message" )

(From http://rhubbarb.wordpress.com/2009/04/08/user-compilation-messages-c-or-c/)


didn't find anything about warning message but MSVC has creates compile errors just like xcode '#error message` according to msdn page


The way it works for me:

#define _message_ "Warning Msg: "
#pragma message(_message_"Need to do something")


You can use #pragma warning ...

Mandatory read


MSVC uses

#pragma error( "message" )

and

#pragma warning( "message" )

directives, whereas GCC leaves out the pragmas.


why don't you use

#warning WarningMessage

or may be

#error ErrorMessage
0

精彩评论

暂无评论...
验证码 换一张
取 消