How can I add an #ifdef DEBUG to Xcode?

33,431

Solution 1

In recent Xcode project templates there’s already a DEBUG=1 macro defined for the Debug build configuration (in the Preprocessor Macros section). You can test it using the #if preprocessor directive.

Solution 2

I usually add my -DDEBUG=1 to the OTHER_C_FLAGS section in my XCode 4 project's build settings.

And yes, they can even discriminate between Debug / Release / ADHOC / Store builds.

Share:
33,431

Related videos on Youtube

nevan king
Author by

nevan king

Mostly questions and answers about iOS & maps

Updated on November 25, 2020

Comments

  • nevan king
    nevan king over 3 years

    I have some code in a project which should never be used in the release build, but is useful when testing. I'd like to do something like this:

    #ifdef DEBUG
        // Run my debugging only code
    #endif
    

    Where do I add the DEBUG setting in Xcode 4? I tried putting it in the "Edit Scheme" under Run MyApp->Arguments Passed On Launch, but it didn't work. Alternatively, is there a flag already available for this?

  • nevan king
    nevan king over 12 years
    I'm working on a project that was created with an Xcode 3 template. Using #if, it still ignores the code. It's possible that I've removed it myself, I'm not sure. Do you know where I can set this up in Xcode 4?
  • Max Steinmeyer
    Max Steinmeyer over 12 years
    It's a configuration, not a scheme. ;)
  • mostafa tourad
    mostafa tourad over 12 years
    You are mixing up scheme and configuration. There commonly is no debug scheme but a run scheme which usually selects the debug configuration.
  • zoul
    zoul over 12 years
    Ah yes, thanks, the whole build thing is getting… complicated :) I’ve edited the post. @nevan, take a look at Build Settings and filter for “preprocessor”. You should see a section called Preprocessor Macros, that’s where you should put a DEBUG=1 line next to the Debug sch—— configuration.
  • nevan king
    nevan king over 12 years
    @zoul Got it, it's working now. Here's the process I used: In the Project->MyApp, under "Build Settings" I filtered for "preprocessor". Hovering over "Preprocessor Macros", I clicked the reveal arrow and it showed up my "Debug, Ad-Hoc, Release" choices. I clicked in the "MyApp" column for Debug and added "DEBUG=1". This automatically copied it to both LLVM and LLVM/GCC settings. I have to agree about the whole Schemes/Configurations thing. Xcode confuses me.
  • nevan king
    nevan king over 12 years
    I'd seen this, but because the choice for "Debug, Ad-Hoc, Release" only show up when you mouse over, I hadn't been able to figure out how to choose one.
  • Ben Gotow
    Ben Gotow over 11 years
    Note that this is only true of iOS apps—the Mac app template does not include DEBUG=1