Automatic #defines according to Debug/Release config in Visual Studio

前端 未结 4 1628
醉梦人生
醉梦人生 2020-12-24 11:07

I have debug output in my program like this:

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


        
相关标签:
4条回答
  • 2020-12-24 11:56

    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.

    0 讨论(0)
  • 2020-12-24 12:00

    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 讨论(0)
  • 2020-12-24 12:10

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

    0 讨论(0)
  • 2020-12-24 12:13

    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.

    0 讨论(0)
提交回复
热议问题