我想在我的代码中加入一些警告或错误。我使用的是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 )https://stackoverflow.com/questions/6438468
复制相似问题