我想在我的代码中加入一些警告或错误。我使用的是visual studio 2010。
我在Xcode中使用了#error和#warning,但visual studio不知道这些指令。
发布于 2012-09-28 10:22:56
在搜索了一些不同的文章后,我终于找到了这个在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 )您可以将其用作:
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 );请注意,TODO()也会生成一个编译器警告;如果您不想将TODO注册为警告,只需使用以下代码:
#define TODO(text) MESSAGE( TODO: text )如果要在警告/错误/待办事项中显示函数名,请使用以下代码:
#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 )发布于 2011-10-05 18:19:03
我知道这个建议有点晚了,但是...
你可以通过下面的技巧达到你想要的效果:
// 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")然后生成一条消息,例如:
#pragma message ( __MSG__ "my message" )(来自http://rhubbarb.wordpress.com/2009/04/08/user-compilation-messages-c-or-c/)
发布于 2011-06-22 18:47:36
我没有找到任何关于警告消息的东西,但是MSVC已经创建了编译错误,就像msdn page所说的xcode '#error message`
https://stackoverflow.com/questions/6438468
复制相似问题