52

I have debug output in my program like this:

#define DEBUG
...
#ifdef DEBUG
    std::cout << "[RE_words] " << m_re << std::endl;
#endif

and DEBUG is defined in my program manually. I always comment out the line when I make a release version. In Visual Studio, there are also Configurations for Debug vs Release versions which handle the commandline etc. used for compiling. Can I also use the Configuration "Debug" to automatically define DEBUG to the compiler? How?

4 Answers 4

92

The Visual Studio automatically defines _DEBUG symbol for Debug builds (and NDEBUG for non-debug builds).

Another way to do this is to go to the project settings -> configuration properties -> C/C++ -> preprocessor, and edit the preprocessor definitions manually.

See also:
This answer explains the differences between _DEBUG and NDEBUG in more detail.
This answer explains the purpose of the NDEBUG symbol and whether or not is it defined by the standard.

4
  • 8
    Incidentally, NDEBUG is the portable solution. It is kind of unfortunate that no standard symbol exists for DEBUG builds. Jan 5, 2011 at 13:53
  • Hi, how can you be sure that vs defines those macros? on msdn it just mention _DEBUG, not NDEBUG, where can I see a list with all the macros VS does for you please? Jul 10, 2013 at 15:23
  • @Icebone1000: I have added a link to an answer explaining whether or not is NDEBUG a standard symbol and why would it be considered portable. See the list of predefined macros in the VS. Jul 17, 2013 at 11:03
  • Note that if you edit the definitions manually, you might need to restart VS before they are recognised.
    – RoG
    Jul 13, 2017 at 11:29
11

Use _DEBUG. Visual C++ defines this for a Debug configuration. Check out the preprocessor directives for the Debug Configuration in your project's properties dialog.

5

I too thought I just had to look at the preprocessor property and remove _DEBUG. Visual Studio tries to help out by setting _DEBUG if you select one of the debug run-time library options.

On the project property page Configuration Properties\C/C++\Code Generation the option selected for Runtime Library affects several defines. When selecting a debug library (/MTd or /MDd) the _DEBUG define is set.

See MSDN /MD, /MT ... for more info on the switches. There are several #defines that are set based on these options. They are pretty invisible when trying to find who sets a #define before you even include any header files!!

0

Yes, you should check project options page, Compile->Advanced compile options. However, VS by default automatically defines DEBUG directive for Debug mode.

1
  • 2
    I believe it's the _DEBUG directive. Note the underscore. Aug 12, 2014 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.