I’m working on C++ program built by other people, and saw a lot of uses of DEBUG like this
#ifdef DEBUG
cout << "Value is "<< value << endl;
#endif
I myself am still in the learning process to become an affluent C++ programmer, and I majorly use Visual Studio and breakpoints to debug. So I’m wondering, if I’m able to step through the code to debug values, is there any other reason to use these kind of macros?
Tried to google but didn’t find much useful page.
Thanks.
Sometimes you don’t want to step through the whole code, but just inspect the output in the terminal.
If the code is compiled with
DEBUGdefined, probably in a debug build, you see the output. For a release build, you don’t. If you go to project settings -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, you’ll see thatDEBUGis defined for the debug build, but it’s not for release. (I actually have_DEBUG)Imagine you have a huge function, and what you’re interested in is at the 1000th line (it’s old code, can’t change it). Would you rather step through all of that messy legacy code or have helpful debug statements at key points? Would you rather the console tell you where something went wrong, or set breakpoints at each of the 237
returnstatements at fail locations?