Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <pre><code> #ifdef _DEBUG printf("Debug statement here, or other piece of code"); #endif </code></pre> <p>This <code>printf</code> statement will only be included in debug build.</p> <p>See <a href="https://stackoverflow.com/questions/2290509/debug-vs-ndebug">this question</a> about differences between these two.</p> <p>More explanation: Before cpp file actually compiler, it is passed to a program which is named preprocessor. Its task is to prepare file for compiling by, for example, replacing <code>#include</code> directives with the appropriate file's content, to replace macros in the code, and so on.</p> <p>One of the things preprocessor is capable to do is to decide will it or will it not include blocks of code wrapped between <code>#if(def) #else #endif#</code> blocks (please note - this is not <code>if/else</code> C++ statement - these are preprocessor directives.</p> <p>Let's say that, for example, you have a program which have a lot of debugging output to do:</p> <pre><code>int foo() { cout &lt;&lt; "Before doing something" &lt;&lt; endl; do_something(); cout &lt;&lt; "After doing something" &lt;&lt; endl; } </code></pre> <p>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:</p> <pre><code>#define PRODUCTION_BUILD int foo() { #ifndef PRODUCTION_BUILD cout &lt;&lt; "Before doing something" &lt;&lt; endl; #endif do_something(); #ifndef PRODUCTION_BUILD cout &lt;&lt; "After doing something" &lt;&lt; endl; #endif } </code></pre> <p><strong>Note:</strong> <code>#ifndef</code> stands for "if not defined".</p> <p>Now, in your debug build, you can just remove <code>#define PRODUCTION_BUILD</code> and the appropriate lines will be included to the file which will be passed to the compiler. In the production build you just need to leave this define, and couts will not be included in the final exe.</p> <p>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). This symbol is named <code>_DEBUG</code> (or <code>NDEBUG</code>, please see the link I left you). </p> <p>Now your code could be just simple as this:</p> <pre><code>int foo() { #ifdef _DEBUG cout &lt;&lt; "Before doing something" &lt;&lt; endl; #endif do_something(); #ifdef _DEBUG cout &lt;&lt; "After doing something" &lt;&lt; endl; #endif } </code></pre> <p><strong>Note:</strong> We are now using <code>#ifdef</code> and not <code>#ifndef</code>.</p> <p>Now your couts will be automatically included only in Debug version (we don't have to <code>#define</code> symbols by ourselves).</p> <p><strong>Note:</strong> This is only for presentation purposes. You don't want to put <code>#ifdefs</code> 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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload