Technical Note TN2339

Building from the Command Line with Xcode FAQ

This document provides answers to frequently asked questions about command line tools.

What is the Command Line Tools Package?
Downloading command-line tools is not available in Xcode for OS X 10.9. How can I install them on my machine?
How do I uninstall my command-line tools?
I have multiple versions of Xcode installed on my machine. What version of Xcode does my command-line tools currently use?
How do I select the default version of Xcode to use for my command-line tools?
How do I build my projects from the command line?
My app has multiple build configurations. How do I set a default build configuration for xcodebuild?
How do I run unit tests in OS X and iOS from the command line?
Document Revision History

What is the Command Line Tools Package?

The Command Line Tools Package is a small self-contained package available for download separately from Xcode and that allows you to do command line development in OS X. It consists of two components: OS X SDK and command-line tools such as Clang, which are installed in /usr/bin.

Downloading command-line tools is not available in Xcode for OS X 10.9. How can I install them on my machine?

In OS X 10.9, the Downloads pane of Xcode Preferences does not support downloading command-line tools. Use any of the following methods to install command-line tools on your system:

Figure 1  Dialog for downloading the tools
Figure 2  Download page for the Command Line Tools package

How do I uninstall my command-line tools?

I have multiple versions of Xcode installed on my machine. What version of Xcode does my command-line tools currently use?

To find out what version of Xcode is being used by your tools, run the following command in Terminal:

$ xcode-select --print-path

Listing 3  Printing the version of Xcode currently used by my tools

$ xcode-select --print-path
/Applications/Xcode5.1.1/Xcode.app/Contents/Developer

How do I select the default version of Xcode to use for my command-line tools?

To select a default Xcode for your command-line tools, run the following command in Terminal:

$ sudo xcode-select -switch <path/to/>Xcode.app

where <path/to/> is the path to the Xcode.app package you wish to use for development.

Listing 4  Setting the default Xcode version

$ sudo xcode-select -switch /Applications/Xcode5.1.1/Xcode.app

How do I build my projects from the command line?

xcodebuild is a command-line tool that allows you to perform build, query, analyze, test, and archive operations on your Xcode projects and workspaces from the command line. It operates on one or more targets contained in your project, or a scheme contained in your project or workspace. xcodebuild provides several options for performing these operations as seen in its man page. xcodebuild saves the output of your commands in the locations defined in the Locations preferences pane of your Xcode application, by default.

See below for various xcodebuild usage. Be sure to navigate to the directory containing your project or workspace in Terminal before running any of the following commands.

My app has multiple build configurations. How do I set a default build configuration for xcodebuild?

In Xcode, the Configuration section of your project Info pane provides a pop-up menu, which sets the default configuration to be used by xcodebuild when building a target. Use this pop-menu to select a default build configuration for xcodebuild as seen in Figure 3.

Figure 3  Debug set as the default build configuration for xcodebuild

How do I run unit tests in OS X and iOS from the command line?

To run your unit tests from the command line, execute the following command in Terminal:

xcodebuild test -scheme <your_scheme_name> -destination destinationspecifier

xcodebuild uses the test build action to run unit tests. This build action requires specifying a scheme and a destination. The -destination option allows you to specify a destination for your unit tests. It takes an argument -destinationspecifier, which describes the device, simulator, or Mac to use as a destination. It consists of a set of comma-separated key=value pairs, which are dependent upon the the device, simulator, or Mac being used.

The -destination option also allows you to run the same unit test on multiple destinations. This is done by adding it multiple times to your xcodebuild test command as demonstrated in Listing 15.

Listing 15  Testing the MyiOSApp scheme in both the Simulator and on an iPod touch

xcodebuild test -scheme MyiOSApp -destination 'platform=iOS Simulator,name=iPhone Retina (4-inch 64-bit),OS=7.1' -destination 'platform=iOS,name=iPod touch'


Document Revision History


DateNotes
2014-05-21

New document that provides answers to frequently asked questions about command-line tools.