Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [6]
If all goes well, you’ll be rewarded with the following:
If not, you’ll get this:
What do you do then? Carefully compare your code with the code in the book. Look for typos and missing semicolons. Xcode will highlight the lines it thinks are problematic. After you find the problem, click the Run button again. Repeat until you have a successful build.
(Don’t get disheartened when you have failed builds with this code or with any code you write in the future. Making and fixing mistakes helps you understand what you’re doing. In fact, it’s actually better than lucking out and getting it right the first time.)
Once your build has succeeded, find the item at the top of the log navigator labeled Debug AGoodStart. Click this item to display the log from the most recent run of your program.
The log can be quite verbose. The important part is the Dickens quote at the end. That’s your code being executed!
GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Tue Jul 5 07:36:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".tty /dev/ttys001
[Switching to process 2723 thread 0x0]
It was the best of times.
It was the worst of times.
(As I’m writing this, Apple is working on a new debugger called LLDB. Eventually it will replace GDB, the current debugger. If you aren’t seeing all the GDB information, it means that LLDB is now Xcode’s standard debugger. The future must be a terrific place; I envy you.)
So what is a program?
Now that you’ve built and run your program, let’s take a look inside. A program is a collection of functions. A function is a list of operations for the processor to execute. Every function has a name, and the function that you just wrote is named main. There was also another function – printf. You didn’t write this function, but you did use it. (We’ll find out where printf comes from in Chapter 5.)
To a programmer, writing a function is a lot like writing a recipe: “Stir a quart of water slowly until it boils. Then mix in a cup of flour. Serve while hot.”
In the mid-1970’s, Betty Crocker started selling a box containing a set of recipe cards. A recipe card is a pretty good metaphor for a function. Like a function, each card has a name and a set of instructions. The difference is that you execute a recipe, and the computer executes a function.
Figure 2.5 A recipe card named Baked Chicken
Betty Crocker’s cooking instructions are in English. In the first part of this book, your functions will be written in the C programming language. However, a computer processor expects its instructions in machine code. How do we get there?
When you write a program in C (which is relatively pleasant for you), the compiler converts your program’s functions into machine code (which is pleasant and efficient for the processor). The compiler is itself a program that is run by Xcode when you press the Run button. Compiling a program is the same as building a program, and we’ll use these terms interchangeably.
When you run a program, the compiled functions are copied from the hard drive into memory, and the function called main is executed by the processor. The main function usually calls other functions. For example, your main function called the printf function. (We’ll see more about how functions interact in Chapter 5.)
Don’t stop
At this point, you’ve probably dealt with several frustrations: installation problems, typos, and lots of new vocabulary. And maybe nothing you’ve done so far makes any sense.