Wednesday, June 13, 2012

Target in Xcode explained

In many development platform, a target is the configuration required to build the source code to make it run on the selected hardware platform.

You may have to configure different parameters like the compiler, the optimization level, whether its for debug or not etc.

In Xcode, there is no much different from this normal definition of Target, but XCode also includes source files in to this definition.

A target in Xcode is a build configuration for a platform in which the code will be executing plus the the software that goes in to it.  Because the software is also included in the Target, this is the reason why you specify a target for each of the file you add in to a project (If you have not noticed it, check carefully next time while you are adding a file)

In Xcode, you can create multiple target for a project, each may have different build configuration, different target and also different set of source files.

Ok, lets explain this with one example,

Create a Max OSX command line project. 

  1. On Xcode, create click File->New->Project
  2. Under the Mac Ox X section on the left side, click Application and on the right side select Command Line Tool and click Next
  3. Give a ProductName (say LearningTarget) and a Company Identifier (say com.avantaj.sample
  4.  On Type select Foundation and for this example it docent matters whether you checked the Use Automatic Reference Counting or not
  5. Click Next
  6. Choose a folder where you need to save the project and click Create

Figure 1: Project Navigator
On the Project navigator, on the left hand side of the Xcode window, you will see something like this.

Under the Products folder you can see the Target created for this Project, LearningTarget.



Build Configurations 

Click on the LearningTarget project, on the top of the list.

On the right side you can see two columns. The first one showing the PROJECT and TARGETS and one on the right hand side showing the build configuration for the selected  PROJECT or TARGETS.

Figure 2: Build settings


Click the project LearningTarget, from the PROJECT section. There you can see the build configuration for the project.

Click the target LearningTarget from the TARGETS section. Here you can see the build configuration for the selected target.

If you see carefully, in this case you will not see any difference between the build configuration of the Project and build configuration of the Target. By default, the build configuration of the target is same as build configuration for the project. You can change the configuration for the particular Target.

Association between a file and a Target

In the window (shown in the above image) click the Target LearningTarget in the section TARGETS. Click Build Phases tab and under that click the section Compile Sources. There you can see all the source files associated with the target LearningTarget.

Figure 3: Build Phases



Try unlinking the main.m from the target LearningTarget. For that click on main.m on the Build Phases tab and click '-' icon shown at the bottom of Compile Source section.

Now clean (Product->clean from the Xcode menu), build and run again. Since now you don't have a main function on the Target, the Xcode will give you the error SBTarget is invalid

Add back the main.m to the target by clicking '+' icon shown at the bottom of Compile Source section.
Clean, build and run again. Everything should be fine now.  


Figure 4:File Inspector


Alternatively, you can also click on a file to see the which all target the file is associated with. 

Click the file main.m at the Project navigator, on the File inspector (right side first tab) select the section Target Memberships. Here you will see in which all target this file is associated with. 

The checkbox can be used to associate and disassociate the file with the targets. 

Adding a target

From the Project Navigator, select the project LearnTarget. You will see a screen shown in Figure 2. At the bottom, you can see Add Target link with a + icon. 

Click it an choose Mac OSX Application->CommandTool and click Next. 
Give a name for the target in the Product Name, say MyNewTarget. Choose Foundation as Type and and click Finish. 

Notice that under the Products group in Project Navigator, you can see the target MyNewTarget.

Also notice that a new group MyNewTarget is created in the Project Navigator. This group has a new main.m file for the newly created target. 

Selecting a Target to execute. 

You can choose the target for execration from the Scheme button at the top of the Xcode window. 


The scheme button has two part. On the left side you can chose any of the targets you have created and on the right side you can chose the run destination. 

Select an target and a run destination and click Run to execute the code. 

You can also edit the properties of the scheme by selecting Product->Edit Scheme from the Xcode menu. 


1 comment: