Though we’ll be doing other interesting things from the command line all semester, it seems a little tedious to have to open up a terminal to compile a program. We can in fact have XCode do all the work for us.
You should have installed XCode from your OS X install discs or the iPhone SDK by now. All the Apple Developer related tools will be located in /Developer (not /Applications). That is, if you click on your hard drive in the Finder, you should see a folder there called Developer. Inside this folder are many, many directories and subdirectories. The Examples folder is particularly interesting…
Inside of /Developer/Applications you’ll find most of the tools you’ll be using this semester. XCode is in there, as well as Interface Builder and Instruments (which is used for code profiling).
Launch XCode. You should get some kind of introductory message. If you’ve used Eclipse, then you have an idea of what to expect. There are many, many things that XCode can do, most of which we won’t get into during the course of the semester. XCode is not Eclipse, and if you like Eclipse you should get any preconceptions out of your mind now. XCode is a very fancy text editor built on top of GCC (GNU C Compiler), and GDB (GNU Debugger). If you’ve never used an IDE (Integrated Development Environment) before, than you might be overwhelmed. Don’t be, remember, the motto of this class is controlling complexity.
This tutorial doesn’t go over creating a Cocoa project. This will be the assignment for the following class. It is well documented. If you have the Cocoa Programming book, I recommend going over Chapter 2. It will seem strange, but that at least walks you through the basics. If you don’t have that book yet you should go over the Currency Converter Tutorial at the Apple Developer site. In fact, I would go over both, because they emphasize different concepts.
For this tutorial what we are interested in is quickly creating little projects for testing the various new concepts we will encounter. In my experience, you will learn very little from just reading about something. In order to get a real “physical” sense of a concept you must work through what is being described.
So instead of creating a project, we’re going to use XCode’s Organizer window. This also happens to be the place where iPhone related XCode info/management lives. But that’s for later…
So close any greeting window that XCode might have presented and click on the Window menu in the XCode application menu bar at the top of the screen. There should be menu item called Organizer. Select it.
You should now see a window that looks something like this.
The Organizer can be used like a mini XCode. If you’ve built a Cocoa project before, the top menu items look somewhat similar. Apple has decided to embrace the iTunes paradigm for most of XCode. Let’s create a directory where we’ll store all our useful snippets of Objective-C code. Click on the plus button at the lower left of the window. Select New Folder. You’ll be presented with a create folder dialog, go ahead and create a folder called ObjC on your Desktop. Select your new folder in the Organizer and click the plus button again, this time selecting New File. We’re going to create yet another hello world project, add a file called hello.c. The text editor on the right should now reflect the fact that you’re looking at a blank file. Go ahead and paste your hello world code from last week’s tutorial into this file.
In the menu we have Build, Clean, and Run. These three actions are important. They are in fact, at the root of any higher level abstraction that XCode might present to you about project management. We going to add little shell scripts for each of these phases.
Click and hold on Build. You should get a drop down menu, select Edit Actions. In the modal dialog that appears click on the plus sign in the lower left corner and select New Shell Script. This will create a piece of text that will be run as if you typed it yourself into Terminal.app. You should get an empty shell script on the list. Double-click on the name to rename to “Build hello”. To the right of the list is where the text of the shell script will go. Type in the following:
gcc -arch i386 -g -Wall -o hello hello.c
By now you should know what some of these flags mean. If you don’t, go find out. Make sure the drop down menu above your freshly minted script has the option Defining Organizing Item selected. Then select OK.
Now if you click and hold on build you should see your new shell script select. Click it. A Build Results window should open saying that the file successfully compiled. Nice.
You program has not run yet. Just compiled. Let’s add a script to run it. Click and hold on the Run button and select Edit Actions. Add a new shell script like we did before and call it Build hello. In the Command text field to the right of the actions list type the following:
./helloAgain make sure the Defining Organizing Item is selected in the drop down menu item above. Click OK. Now click and hold on the Run button and make sure your new action is select. Click run. Hmm, nothing happened.
Well something did happen, but you just didn’t see it. You need to open the XCode Console window to see output from program. This is also where NSLog’s will appear (Objective-C version of printf or Java’s System.out.println for debugging). You can open the Console window by click on the XCode Run menu (on the main application menu bar, not the Organizer window). Do this now. You should see your output there.
You should do the Objective-C tutorial next.
