Visual C++ CRT debugging -
hello explain me point in using define debug, ndebug... , can provide me example. idea idea, help, suggetion appreciated.
so point in: #define debug or #define ndebug
these preprocessor definitions inserted compiler in debug build, utilize it. example, may want print debug statements in debug build, , not include them in release build. can achieve using preprocessor:
#ifdef _debug printf("debug statement here, or other piece of code"); #endif this printf statement included in debug build.
see this question differences between these two.
more explanation: before cpp file compiler, passed program named preprocessor. task prepare file compiling by, example, replacing #include directives appropriate file's content, replace macros in code, , on.
one of things preprocessor capable decide or not include blocks of code wrapped between #if(def) #else #endif# blocks (please note - not if/else c++ statement - these preprocessor directives.
let's that, example, have program have lot of debugging output do:
int foo() { cout << "before doing something" << endl; do_something(); cout << "after doing something" << endl; } now, don't want these prints appear in production executable. first may go , comment these lines manually. second approach can rely on preprocessor automatically 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 "if not defined".
now, in debug build, can remove #define production_build , appropriate lines included file passed compiler. in production build need leave define, , couts not included in final exe.
now, there bunch of predefined symbols compilers can generate. 1 windows version, , 1 if compiling code debugging symbols or not (debug vs release version). symbol named _debug (or ndebug, please see link left you).
now code simple this:
int foo() { #ifdef _debug cout << "before doing something" << endl; #endif do_something(); #ifdef _debug cout << "after doing something" << endl; #endif } note: using #ifdef , not #ifndef.
now couts automatically included in debug version (we don't have #define symbols ourselves).
note: presentation purposes. don't want put #ifdefs in every few lines, may want, example provide function won't in release build (will have empty body), , logging in debug version.
Comments
Post a Comment