Automatic #defines according to Debug/Release config in Visual Studio
Solution 1
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.
Solution 2
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.
Solution 3
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!!
Related videos on Youtube
Felix Dombek
Computational Linguistics B.Sc. from University of Potsdam, Germany. 7 years of experience developing Windows software for a backup/office software company (languages: classic and modern C++, VB6, Python, PHP) 4 years at TomTom, developing embedded routing software on Linux in modern C++.
Updated on October 09, 2020Comments
-
Felix Dombek over 3 years
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 defineDEBUG
to the compiler? How? -
edA-qa mort-ora-y over 13 yearsIncidentally, NDEBUG is the portable solution. It is kind of unfortunate that no standard symbol exists for DEBUG builds.
-
Icebone1000 almost 11 yearsHi, 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?
-
Jan Holecek almost 11 years@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.
-
notbad.jpeg over 9 yearsI believe it's the
_DEBUG
directive. Note the underscore. -
RoG almost 7 yearsNote that if you edit the definitions manually, you might need to restart VS before they are recognised.