Custom compiler flags in XCode

So I’ve been messing with compiler flags in XCode for the last hour or so, and it turns out I was totally misunderstanding things. If you get info on a project or target in XCode, there’s a “User-Defined” section at the bottom that allows you to create your own flags for use at compile time. However, these flags are not actually passed into GCC! (Mistake 1!) I assumed that adding a key-value pair to the list would pass the flag straight through to GCC, but it appears that they are for use in build scripts, etc…

To pass a custom flag through to GCC so that you can use it in #ifdef and #if macros at compile time, you have to add a User-Defined Setting called “OTHER_CFLAGS”. For my project, I set the value to “-DIS_PHOTO_CHAT=1″. At compile time, the exact text is passed as an argument to GCC. You can set different values in different targets - and I was able to conditionally include some code in a header file using #if (IS_PHOTO_CHAT==1)…

It seems like you can use XCode’s built-in flags like ${TARGET_NAME} and ${PRODUCT_NAME} to insert variables into the value of OTHER_CFLAGS, but if your target name has spaces, I think you’re at a loss. I tried to set -DTARGET_NAME=${TARGET_NAME} for about an hour, but the target name had a space and I can’t get GCC to accept the value (tried quotes… no luck…) I’m no command-line-compiler-whiz, so I’m sure there’s a trick, but Google hasn’t turned anything up.

I’m still surprised OTHER_CFLAGS wasn’t preset to “” in the target build settings. There’s an empty field for “Other Code Signing Flags” (which seems less useful!) Oh well… Guess XCode is in permanent beta anyway?

  1. You’re looking for GCC_PREPROCESSOR_DEFINITIONS. Add this under the User-Defined section. The definition should be like: DEBUG=1 FLARTMODE=2

  2. You’re mistaking build settings for compiler flags. It’s as if you thought that setting FOO = BAR in a makefile would be passed to the compiler automatically.

    There are a bunch of build settings. Many translate directly to compiler flags, but most do other things in the build system. There is one specific build setting (in the UI, it’s cryptically titled “Preprocessor Macros”) that sets the initial value for preprocessor macros. And there’s another one (in the UI, also mysteriously called “Other C Flags”) that lets you send any arbitrary flag to the compiler.

    http://developer.apple.com/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW13

    http://developer.apple.com/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html#//apple_ref/doc/uid/TP40003931-CH3-SW17

    There are some pretty active forums on Apple’s devforums.apple.com boards, on the xcode-users@lists.apple.com mailing list, and on Stack Overflow where basic questions like this can be answered pretty quickly without banging your head against a wall.

  3. Ben Gotow

    Hey Chris,

    Thanks for the help! I’ll have to clean up this post tomorrow. For some reason I had a hard time finding the documentation for XCode. Reading through the docs, I was definitely confused about the difference between build settings and compiler flags. Thanks for pointing me in the right direction - I’ll definitely look to StackOverflow or the mailing list next time something like this comes up. The “Preprocessor Macros” and “Other C Flags” in the UI make sense, but they’re pretty easy to gloss over!

Reply to 'Custom compiler flags in XCode'