views:

2279

answers:

3

In c# I can use the following code to have code which only executes during debug build, how can I do the same in xcode?

if #DEBUG
{
    // etc etc
}
+6  A: 

You can use

#ifdef DEBUG
    ....
#endif

You'll need to add DEBUG=1 to the project's preprocessor symbol definitions in the Debug configuration's settings as that's not done for you automatically by Xcode.

I personally prefer doing DEBUG=1 over checking for NDEBUG=0, since the latter implies that the default build configuration is with debug information which you then have to explicitly turn off, whereas 'DEBUG=1' implies turning on debug only code.

Alnitak
+7  A: 

The NDEBUG symbol should be defined for you already in release mode builds

#ifndef NDEBUG
/* Debug only code */    
#endif

By using NDEBUG you just avoid having to specify a -D DEBUG argument to the compiler yourself for the debug builds

ShuggyCoUk
but you still have to _add_ NDEBUG to the compiler settings to use release mode. Many programmers prefer #ifdef DEBUG to the double-negative that's in #ifndef NDEBUG
Alnitak
I get that many people prefer it but NDEBUG for release is the C standard. By adhering to it you will save yourself trouble. NEDBUG should also be defined for you already in the Xcode release builds
ShuggyCoUk
My apologies XCode doesn't appear to define it by default. here's where you should go to add it in: http://developer.apple.com/tools/xcode/xcodebuildsettings.html
ShuggyCoUk
p.s. what if he wants to decouple the behaviour of assert() from his own debugging code?
Alnitak
Absolutely if he wishes to take control himself he will need to make a concious decision and define accordingly. This appears to be the rationale of the XCode implementers in not providing some default definitions in the way VS and others do. I simply though that he should start with the standard.
ShuggyCoUk
+1  A: 

There is a very useful debugging technote: Technical Note TN2124 Mac OS X Debugging Magic http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV which contains lots of useful stuff for debugging your apps.

Tony

Tony Lambert