Remove Debug Code For Release Build

When it’s time to remove debug code and log messasges from your application, it’s straight forward if you follow a few simple steps during development.

First, wrap debugging code in a block similar to the following:

#ifdef DEBUG
  // Debug code here...  
#endif

From the Edit Scheme dialog select the Run tab. In the Info tab, set the Build Configuration to Debug. By the way, when you create a new project, Debug is the default value, so you can skip this step if you have not previously changed the value.

When you are ready to have the debug code removed from your build, return to the Edit Scheme menu and set the Build Configuration to Release. When you rebuild the project, the code inside the #ifdef DEBUG – #endif will no longer be included in the app.

You can see the preprocessing macro definitions that make this possible by opening the Build Settings and searching for debug. You will find the DEBUG macro defined as follows:

Removing Console / NSLog Messages

In a previous post, Debugging Macros, I wrote an alternative for NSLog. Along with this, I included code to turn those same messages off. However, there is now a much cleaner way to do this using the DEBUG preprocessor macro shown above:

#ifdef DEBUG
  #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
  #define debug(format, ...) 
#endif

You can now write debugging messages to the console as follows, which will be removed when creating a release build:

debug(@"Your debug message here...");
  1. Hey,

    What about #ifdef __OPTIMIZE__. I use it in different projects and it does not require to put/set any debug flag since it is already present in a release configuration.

    Regards,

    K.

  2. good stuff. thanks. i would suggest we test a few rounds with the Release build configuration before releasing.

  3. I have two different schemes set up: one for Debug and one for Release. The Debug scheme has everything set to the Debug build configuration, and all of the debugging bells and whistles (GuardMalloc, etc.). Release is set up similarly (with all of the debugging stuff off). This makes it super easy to switch between the two configurations (and keeps the Schemes from becoming confusededed when it seems to lose track of built application paths).

  4. This macro doesn’t work in ARC enabled project.
    I am getting following error:

    Implicit conversion of Objective-C pointer type ‘id’ to C pointer type ‘CFTypeRef’ (aka ‘const void *’) requires a bridged cast

    I have added the macro in my .pch file…

  5. When working with ARC, you need to bridge the CFShow parameter…

    #define debug(format, …) CFShow((__bridge void *)[NSString stringWithFormat:format, ## __VA_ARGS__]);

    I hope this helps.

Comments are closed.