简体   繁体   English

Visual C ++ CRT调试

[英]Visual C++ CRT debugging

Hello could anyone explain to me what is the point in using define DEBUG, NDEBUG... and can provide me with an example. 您好,有人可以向我解释使用define DEBUG,NDEBUG ...有什么意义,并可以为我提供示例。 any idea idea, help, suggetion is appreciated. 任何想法,帮助,建议都值得赞赏。

so what is the point in: #define DEBUG or #define NDEBUG 那么,什么是重点: #define DEBUG#define NDEBUG

These preprocessor definitions are usually inserted by the compiler in debug build, for you to utilize it. 这些预处理器定义通常由编译器插入调试版本中,以供您使用。 For example, you may want to print debug statements only in debug build, and not to include them in release build. 例如,您可能只想在调试版本中打印调试语句,而不在发行版本中包含它们。 You can achieve that by using preprocessor: 您可以通过使用预处理器来实现:

 #ifdef _DEBUG
     printf("Debug statement here, or other piece of code");
 #endif

This printf statement will only be included in debug build. printf语句仅包含在调试版本中。

See this question about differences between these two. 请参阅有关两者之间差异的问题

More explanation: Before cpp file actually compiler, it is passed to a program which is named preprocessor. 更多说明:在cpp文件实际编译之前,它会传递给名为预处理程序的程序。 Its task is to prepare file for compiling by, for example, replacing #include directives with the appropriate file's content, to replace macros in the code, and so on. 它的任务是准备要编译的文件,例如,用适当的文件内容替换#include指令,替换代码中的宏,等等。

One of the things preprocessor is capable to do is to decide will it or will it not include blocks of code wrapped between #if(def) #else #endif# blocks (please note - this is not if/else C++ statement - these are preprocessor directives. 预处理程序能够执行的操作之一是确定是否包含在#if(def) #else #endif#块之间包装的代码块(请注意-这不是if/else C ++语句-这些是预处理程序指令。

Let's say that, for example, you have a program which have a lot of debugging output to do: 举例来说,假设您有一个程序需要执行大量调试输出:

int foo()
{
    cout << "Before doing something" << endl;
    do_something();
    cout << "After doing something" << endl;
}

Now, you don't want these prints to appear in the production executable. 现在,您不希望这些打印品出现在生产可执行文件中。 First is that you may go and comment all these lines manually. 首先,您可以手动注释所有这些行。 Second approach is that you can rely on preprocessor to do this automatically for you: 第二种方法是您可以依靠预处理器为您自动执行此操作:

#define PRODUCTION_BUILD

int foo()
{
    #ifndef PRODUCTION_BUILD
        cout << "Before doing something" << endl;
    #endif

    do_something();

    #ifndef PRODUCTION_BUILD
        cout << "After doing something" << endl;
    #endif
}

Note: #ifndef stands for "if not defined". 注意: #ifndef代表“如果未定义”。

Now, in your debug build, you can just remove #define PRODUCTION_BUILD and the appropriate lines will be included to the file which will be passed to the compiler. 现在,在调试版本中,您只需删除#define PRODUCTION_BUILD ,相应的行将包含在文件中,该文件将传递给编译器。 In the production build you just need to leave this define, and couts will not be included in the final exe. 在生产版本中,您只需要保留此定义,并且最终可执行文件中将不包含提示。

Now, there is a bunch of predefined symbols that compilers can generate. 现在,编译器可以生成一堆预定义的符号。 One could be your Windows version, and one could be if you are compiling your code with debugging symbols or not (Debug vs Release version). 一种可能是Windows版本,一种可能是是否使用调试符号编译代码(Debug vs Release版本)。 This symbol is named _DEBUG (or NDEBUG , please see the link I left you). 该符号名为_DEBUG (或NDEBUG ,请参阅我离开您的链接)。

Now your code could be just simple as this: 现在,您的代码可能非常简单,如下所示:

int foo()
{
    #ifdef _DEBUG
        cout << "Before doing something" << endl;
    #endif

    do_something();

    #ifdef _DEBUG
        cout << "After doing something" << endl;
    #endif
}

Note: We are now using #ifdef and not #ifndef . 注意:我们现在使用#ifdef而不是#ifndef

Now your couts will be automatically included only in Debug version (we don't have to #define symbols by ourselves). 现在,您的提示将仅自动包含在Debug版本中(我们不必自己#define符号)。

Note: This is only for presentation purposes. 注意:这仅用于演示目的。 You don't want to put #ifdefs in every few lines, but you may want, for example to provide a function which will won't do anything in the Release build (will have empty body), and it could do logging in Debug version. 您不想在每隔几行中插入#ifdefs ,但是您可能想要例如提供一个将不会在Release版本中做任何事情的函数(主体为空),并且可以在Debug中进行记录版。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM