views:

8093

answers:

7

In all the examples I've seen of the #if compiler directive, they use "DEBUG". Can I use "RELEASE" in the same way to exclude code that I don't want to run when compiled in debug mode? The code I want to surround with this block sends out a bunch of emails, and I don't want to accidentally send those out when testing.

+33  A: 

Don't know if RELEASE is defined, but you can use

#if (!DEBUG)
M4N
It isn't, but of course you can use the NOT operator like that. Didn't think of that when I wrote my answer.
Lasse V. Karlsen
It does, but using your solution is clearer and "more correct", IMHO.
Daniel Schaffer
Thanks guys! I think this answer is what I will use, but I accepted lassevk's answer since it was more thorough.
Brian Sullivan
+28  A: 

No, it won't, unless you do some work.

The important part here is what DEBUG really is, and it's a kind of constant defined that the compiler can check against.

If you check the project properties, under the Build tab, you'll find three things:

  • A text box labelled "Conditional compilation symbols"
  • A check box labelled "Define DEBUG constant"
  • A check box labelled "Define TRACE constant"

There is no such checkbox, nor constant/symbol pre-defined that has the name RELEASE.

However, you can easily add that name to the text box labelled Conditional compilation symbols, but make sure you set the project configuration to Release-mode before doing so, as these settings are per configuration.

So basically, unless you add that to the text box, #if RELEASE won't produce any code under any configuration.


Edit: Corrected #ifdef to #if (thanks Daniel)

Lasse V. Karlsen
Hey isn't #ifdef C/C++? For C#, it's just #if
Daniel Schaffer
+5  A: 

On my VS install (VS 2008) #if release does not work. However you could just use !DEBUG

Example:

#if !DEBUG
SendTediousEmail()
#endif
JaredPar
+4  A: 

Nope.

While in debug configuration there is a DEBUG defined constant (automatically defined by Visual Studio) while there is no such constant defined for release mode. Check your project settings under build.

P.S.

Selecting [Define DEBUG constant] under Project -> Build is like including #define DEBUG at the beginning of every file.

PS2.

If you want to define a RELEASE constant for the release configuration go to:

  • Project Properties -> Build
  • Select Release Mode
  • in the Conditional compilation symbols textbox enter: RELEASE
Pop Catalin
A: 

why not just

#if RELEASE
#undef DEBUG
#endif
Matt Davison
There's no constant named RELEASE defined by Visual Studio under release mode.
Pop Catalin
uhh...#define RELEASE
Matt Davison
+4  A: 

I've never seen that before...but I have seen:

#if (DEBUG == FALSE)

and

#if (!DEBUG)

That work for ya?

Gus
A: 

"Pop Catalin" got it right. Controlling the definition based on the type of build provides a great deal of flexibility. For example, you can have a "DEBUG", "DEMO", and "RELEASE" configuration all in the same solution. That prevents the need for duplicate programming with two different solutions.

So yes #if RELEASE or #if (RELEASE) works the same as #if DEBUG when the RELEASE Conditional compilation symbol is defined.

The following is taken from "Pop Catalin" post: If you want to define a RELEASE constant for the release configuration go to: * Project Properties -> Build * Select Release Mode * in the Conditional compilation symbols textbox enter: RELEASE