Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

where to defind DEBUG symbol for Debug build in VS2012?

I've Win32 DLL application. Debug build is selected. I wrote such code:

#if DEBUG
fflush(logFile);
#endif

But fflush(logFile); is grayed out so I assume it will not be executed. But i want it to be executed. Does it mean that in Debug DEBUG symbol is not defined? Where can I define it in VS2012?

Upvotes: 1

Views: 2246

Answers (3)

Michael Burr
Michael Burr

Reputation: 340406

By default, a Visual Studio C++ project will have the macro _DEBUG defined for a Debug project configuration. It will have the value 1, so you can test for it using #if or #ifdef.

But note that the macro starts with an underscore - if you want to use the name DEBUG (maybe you have existing code that uses that name), you'll need to add it to the project properties yourself (C/C++ | Preprocessor | Preprocessor definitions). Or you can put the following in a header that's included in every translation unit (maybe stdafx.h):

#if _DEBUG
#undef DEBUG
#define DEBUG 1
#endif

Upvotes: 2

Roman Ryltsov
Roman Ryltsov

Reputation: 69724

Preprocessor definitions are defined under project settings as shown on screenshot (note _DEBUG there):

enter image description here

Note that in case of _DEBUG you want to check if it is defined at all, and not compare it (possibly missing definition) to zero. You want:

#if defined(_DEBUG)

or

#ifdef _DEBUG

Upvotes: 3

Ajay
Ajay

Reputation: 18431

Every project has two builds: Debug and Release. Debug build have DEBUG defined, as if you defined using:

#define DEBUG

It enables, the code to get generated differently. The writers of code (functions, classes etc), may add additional diagonistics to aid in debugging. The Debug build is for debugging only, and you don't give this build (i.e. EXE generated with Debug build), to the customers.

Another build where DEBUG symbols is not defined, is for Release build. A release build is for optmized code, at code level, compiler setting level, and linker level. Most of diagonistic, asserts, debugging-aid feature will be disabled - so as to produce optimized executable.

Whomsoever who has written the above code, has written the same thing in mind. To let flush the file, only if debug build is running. You can comment the #if and #endif, and let fflush line compiled, or you can use Release build. It all depends on you.

Upvotes: 0

Related Questions