Xcode 4: Setting Compiler Flags for a Single File
In Xcode 4 you can set compiler flags for a single file from the target’s Compile Sources build phase.
- Select the project file from the project navigator to open the project editor.
- Select the target from the left side of the project editor.
- Click the Build Phases button at the top of the editor.
- Click the disclosure triangle next to the Compile Sources build phase.

Select the file where you want to set the compiler flags. Double-click the Compiler Flags column to open a pop-up editor, which is where you enter the flags.
Using Makefiles in Xcode 4
Sometimes I read questions from developers who want to use makefiles in Xcode. The short answer to their questions is to create an external build system project. The rest of this article provides a longer answer for those of you using Xcode 4.
Create an External Build System Project
An external build system project uses something besides Xcode to build the project. By using an external build system project you can use another build tool, such as make, ant, CMake, and SCons to build the project. An external build system also lets you use languages Xcode does not directly support. Supply the path to the language’s compiler when creating the project.
Choose File > New > New Project to create a new project. The external build system project is in the Other section under Mac OS X.

After selecting the external build system project, click the Next button to move on to the next step. In the next step you name the project and supply the path to the build tool. Xcode is initially configured to use make so you should not have to change the build tool if you’re using a makefile.

If you’re running Xcode 4.2 or later you will see the Use Automatic Reference Counting checkbox. This checkbox applies only to Objective-C code. If you’re not using Objective-C, deselect the checkbox.
After naming the project, click the Next button. Choose a location to save the project, and click the Create button to finish creating the project.
Add Files to the Project
Now that you’ve created an external build system project, you must add the makefile and source code files to the project. If you have existing files, choose File > Add Files to ProjectName to add the files to the project. Choose File > New > New File to add a blank file to your project. Xcode does not have a makefile file template. Add an empty file to the project to add a new makefile. The empty file template is in the Other section under Mac OS X.
Configuring the Build Tool
If you need to configure the build tool, use the project editor. Choose the project file from the project navigator on the left side of the project window to open the project editor.

Select the target from the target list. Click the Info button at the top of the editor, which you can see in the screenshot.
Build the Project
Click the Run button or use the Product menu to build the project. Xcode builds the project using the makefile. If you chose a different build tool, Xcode uses the tool you specified to build the project.
Xcode 4.1: Modernizing Projects
When you open a project in Xcode 4.1, Xcode checks the project for any outdated settings like build settings, SDKs, and project file formats. If Xcode finds any outdated settings, they appear as a Project Modernization warning in the issue navigator. Select the Project Modernization warning or choose Editor > Modernize Project to update the project.
Checking for outdated settings makes working with old Xcode projects easier. If you have ever downloaded an old Xcode project and been unable to build it due to a missing SDK, you will appreciate Xcode’s ability to modernize a project.
Xcode 4: Unit Testing 64-Bit Mac Apps
When unit-testing a 64-bit Mac application, make sure the class files you are testing (your application’s implementation files) are members of the unit testing target. You will get linker errors when building the unit testing target if the class files being tested are not members of the unit testing target.
How do you make a file a member of the unit testing target? Open the file inspector by choosing View > Utilities > File Inspector. Select the file from the project navigator. Select the checkbox for the unit testing target in the Target Membership section of the file inspector.

Xcode 4: Removing Files from a Project
To remove a file from an Xcode project, select the file from the project navigator and press the Delete key. An alert like the following opens:

If you want to delete the file, the safest thing to do is click the Remove Reference Only button, then move the file to the Trash. Clicking the Delete button permanently deletes the file, which means the only ways to recover the file are to restore a backup or use a data recovery tool. Unless you’re absolutely sure you want to delete the file, click the Remove Reference Only button.
Update: May 2012
I noticed in Xcode 4.3.2 that the Delete button referenced in the screenshot now says Move to Trash. I’m not sure which version of Xcode introduced this change as I don’t delete files from projects often.
Xcode 4: Finding the Application You Built
Previous versions of Xcode created a build folder inside your project folder and placed your application there. Xcode 4 stores build products in the following location:
/Users/Username/Library/Developer/Xcode/DerivedData
When you build the project, Xcode creates a folder for your project in the derived data folder and places the files it creates in that folder. If you want your derived data in a different location, open Xcode’s Location Preferences. Choosing File > Project Settings lets you change the derived data location for a single project.
Update
You can also open the folder where your application resides using the Xcode project window. Click the disclosure triangle next to the Products folder in the project navigator. Select the application, right-click, and choose Show in Finder.
Xcode 4: Finding User Interface Elements
One of Xcode 4′s biggest changes is its integration with Interface Builder. There is no longer a separate Interface Builder application. Select a xib file from the project navigator to create your application’s user interface. You may be wondering where the user interface elements are located in Xcode 4.
They are in the object library. Choose View > Utilities > Object Library to open the object library, which is located in the lower right corner of the project window.

Xcode 4: Unit Testing Support
Developers doing test-driven development with the Cocoa and Cocoa Touch frameworks will like the improved support Xcode 4 has for unit testing. When you create a project that uses the Cocoa or Cocoa Touch frameworks, such as a Cocoa or iPhone application, Xcode gives you the option to add a unit testing bundle to the project.

When you select the Include Unit Tests checkbox, Xcode adds a unit testing bundle and a unit testing class to the project. Xcode also sets the Bundle Loader and Test Host build settings for the unit testing target and creates a target dependency so you can automatically run the unit tests.
Another change Apple made to unit testing in Xcode 4 is adding the Test After Build build setting. When you set the Test After Build build setting to YES, Xcode runs your unit tests after building the unit testing target. Choose Product > Build For > Build For Testing to build the unit testing target. The test results appear in the build results window. Read my Opening the Build Results Window post for more information on opening the build results window. If Test After Build is set to NO, you must run the tests by choosing Product > Test. Open the log navigator to view the test results. I found unit testing works better when setting Test After Build to YES.
If you open the scheme editor (use the Scheme pop-up menu in the project window toolbar), you can specify what tests are run as well as run scripts before and after running the tests. Xcode initially is set to run all tests.
Xcode 4: Splitting the Editor
Xcode 4′s standard editor does not support splitting. Use the assistant editor if you want to split the editor. Choose View > Editor > Assistant to open the assistant editor. Choose View > Assistant Layout to customize the appearance of the assistant editor. Click the Add Assistant Editor button (the + button) on the right side of the jump bar to split the editor.
Xcode 4: Adding Command-Line Arguments and Environment Variables
Choose Edit Scheme from the Scheme pop-up menu in the project window toolbar to open the scheme editor. Select Run from the list on the left side of the scheme editor. Click the Arguments button in the scheme editor.

From here you can add command-line arguments and environment variables. Clicking the Diagnostics button provides checkboxes for commonly-used environment variables.