Once you get smart enough to build multiple apps from the same project you probably have to wrap your head around the concept of #define and #ifdef to conditionally activate and deactivate portions of your code. What is somewhat unnerving is that the target build settings change their appearance seemingly uncontrollably and I took a while to work out what triggers this morphing.
The difference lies in what settings you choose in the upper left hand corner of XCode. Only if you select the “Base SDK” setting then the dialog is the most user-friendly.
The build settings for the active target can no be reached by right-click on target, Get Info, build tab. Or alternatively menu option “Project”, “Edit Active Target”. I discovered three places where #define statements can be placed. I am going to show them to you and make a recommendation as well.
I often read that you should put them in “Other C Flags”. This works because the contents of this setting are appended to the build command. -D is the option that defines additional defines. But actually this is the least recommended method. Again, this “GCC 4.0 – Language” is ONLY VISIBLE if you select the “Base SDK” which I usually set to “iPhone Device 2.0”.
The second place is also the smartest because it is least prone to spelling mistakes if you have multiple defines. If you double-click on the setting you get a nice dialog which permits to add multiple defines. Don’t let yourself be fooled by the name “macros”. Because this is the originally intended place to put those defines you don’t specify the -D. Again, the tricky bit is that this setting is only visible in this form if the “Base SDK” is selected.
The third place is actually the same as the second, but if you select a Simulator setting which during development you will most likely do.It is highly confusing, but the current way XCode works. To get to the “nice UI” just set it back to the base SDK.
This little change causes XCode to show you the real internal settings outside of the nicely made up interface. All of the GCC settings are now mashed together at the bottom in the “User-Defined” section. Ugly!
So here you see what the setting is really being called internally in the left column. In the right column you see the setting value. You can see the OTHER_CFLAGS as well as the GCC_PREPROCESSOR_DEFINITIONS which both correspond to the above “nice UI” settings. If you change them here and switch back to the Base SDK you will find that they ended up in the right place.
Having worked out these mechanisms I recommend that you put your #defines from now on in their rightful place which is the “Preprocessor Macros” setting which you only get to see if you select an iPhone SDK or the Base SDK.
Categories: Recipes
Just today I needed to add a new target for a software that I’m developing and this article apparead on your site 🙂