[英]How to view the output of DEBUG conditional statement?
I have written a code with #ifdef DEBUG
conditional statement to print cout
statement in code blocks. 我已经使用
#ifdef DEBUG
条件语句编写了一个代码,以在代码块中打印cout
语句。 My questions are: 我的问题是:
I am not sure about codeblocks but in visual studio you can select if you want to build the debug or release version of the program (or any other version you define). 我不确定代码块,但是在Visual Studio中,您可以选择是否要构建程序的调试或发行版本(或定义的任何其他版本)。 What this effectively does is that it will set the flag DEBUG to true.
这实际上是将标志DEBUG设置为true。 and you don't need to define a variable manually.
而且您不需要手动定义变量。 In any case you can use your own definition for this.
无论如何,您都可以使用自己的定义。
In the debug version anything inside the #ifdef DEBUG will be also compiled while in the release version these chunks of code will be skipped. 在调试版本中,#ifdef DEBUG内部的所有内容都将被编译,而在发行版本中,这些代码块将被跳过。 To get information from debugging you can define a macro debug print like this.
要从调试中获取信息,您可以像这样定义宏调试打印。
#define DEBUG_MODE 1 // Or 0 if you dont want to debug
#ifdef DEBUG_MODE
#define Debug( x ) std::cout << x
#else
#define Debug( x )
#endif
and then call your Debug( someVariable ); 然后调用您的Debug(someVariable); If you build the debug version you get output in the console otherwise nothing will happen.
如果生成调试版本,则将在控制台中输出,否则将不会发生任何事情。
As mentioned in other comments/answers you can define a macro such as DEBUG(message)
for printing debug messages in debug builds only. 如其他注释/答案中所述,您可以定义一个宏,例如
DEBUG(message)
,仅用于在调试版本中打印调试消息。 However, I suggest you use NDEBUG
instead of DEBUG
to do so. 但是,我建议您使用
NDEBUG
而不是DEBUG
来这样做。 NDEBUG
is a standardized predefined macro that is automatically defined by compiler in release build, if this is your intent. NDEBUG
是一个标准化的预定义宏,如果您打算这样做的话,编译器会在发行版本中自动定义它。 Use it this way : 使用这种方式:
// #define NDEBUG ==> not needed, this macro will be predefined by compiler in release build
#ifdef NDEBUG // release build
# define DEBUG(msg)
#else // debug build
# define DEBUG(msg) std::cout << msg
#endif
int main(void)
{
DEBUG("this will be printed to console in debug build only\n");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.