OpenGL Book Status Update

I’ve received several emails from people inquiring about the status of my OpenGL 2D programming book. What is the book’s status? The OpenGL book is currently on hold while I finish up the Xcode book. When the Xcode book is finished, I’ll return to the OpenGL book.
|

Two Setbacks

People have been emailing me about how things are going on the new edition of Xcode Tools Sensei for Xcode 3. I’m still working on the book, but there have been two setbacks that have slowed me down.

The first setback is health-related. For the past six weeks I’ve been battling an injury to my lower back. Because of the injury, my back stiffens up and causes a lot of pain when I sit for more than 10-15 minutes. This makes writing difficult because writing, like programming, requires blocks of uninterrupted time to be done at its best. I’m not able to finish as much as I could if I were able to sit down for 3-4 hours and crank out some material.

The second setback is Apple’s continuation of the non-disclosure agreement (NDA) on the iPhone SDK. I’m surprised the SDK is still under NDA after the opening of the iPhone App Store, but the NDA still stands, and it’s causing me two problems. First, I can’t ask any questions about the SDK, which makes writing the iPhone material take longer than it normally would. I can live with that, but I can’t live with the second problem. I can’t release the book until the NDA is lifted. Even if I had the book finished today, you couldn’t read it. I have no idea when Apple will lift the NDA.
|

Avoiding Xcode

Xcode provides a front end for command-line tools like the GCC compiler and the GDB debugger. What this means is you can avoid Xcode and use the command-line tools if you want. There are several situations where you might want to bypass Xcode. If you’re learning C or C++, you’ll start out by writing small programs that contain one source code file. Invoking GCC directly would be easier for you than creating an Xcode project, especially if you’re familiar with the Unix command line. People writing cross-platform applications might want to run build scripts so their application builds the same way on multiple operating systems. Or you just might hate Xcode.

This post shows you how you can program on Mac OS X and avoid Xcode. By avoiding Xcode, I mean not having to use it. You still have to install Xcode. I’m sure it’s possible to install gcc without installing Xcode, but you’ll save yourself time and aggravation by just installing Xcode and not using it.

Writing and Compiling Your Code

Write your source code in the text editor of your choice. There are many text editors available for Mac OS X. TextWrangler and Smultron are free. TextMate, BBEdit, and ForgEdit aren’t free. If you search the Internet, I’m sure you’ll find more editors.

When you’ve finished writing your code, it’s time to compile it into a running program. Launch the Terminal application and navigate to the directory where your source code files are. Now you can compile your code. The simplest way to compile your code with GCC is to use the -o option. It takes the following form:

gcc -o AppName FilesToCompile (C programs)
g++ -o AppName FilesToCompile (C++ programs)

If you wrote a Hello World program in C++ and saved your file as main.cpp, you would enter the following command to compile your program:

g++ -o HelloWorld main.cpp

GCC will compile your code and create an executable file named HelloWorld. You can run the program by double-clicking the executable file in the Finder.

Makefiles

Running GCC directly is fine when you’re writing small programs, but problems arise with larger programs. Imagine you’re writing a program that has 12 source files, links to four libraries, and uses six compiler flags. Having to type in the names of the source files, libraries, and compiler flags every time you want to compile your program would be tedious and error prone.

The solution is to use a makefile, which is a text file. Instead of running GCC directly, you put everything you need to compile your program in the makefile: source file names, compiler flags, paths to libraries, and paths to headers. Run the make command to compile your program using the makefile.

I’m not an expert on makefiles so I’m not going to show you how to create a makefile, but there are many tutorials on the Internet. Do a Google search for makefiles and you’ll find the tutorials.

If you don’t want to use makefiles, there are several cross-platform, open source build systems available. Three of the more popular systems are SCons, CMake, and autoconf. Those of you writing larger programs should look into these build systems if you don’t want to use Xcode.
|

Every Xcode Program Needs a Project

I’m not sure why, but an old blog post about writing C++ programs on Mac OS X has been generating a lot of activity recently. In the comments about the blog post, I got a question that is fairly common among people who are new to Xcode. They start Xcode, create a file and write some code. They want to compile the code and run the program they wrote, and either they don’t know how to compile it or they find that all the menu items in the Build menu are disabled. If you find yourself in a similar situation, a question comes to mind.

Why Can’t I Compile My Code?

The reason you can’t compile the code is that Xcode deals with projects. You can’t just create a source code file in Xcode and compile it into a working program. If you want to use Xcode to write a program, you must create a project.

But My Program Is Only a Few LInes of Code!

It doesn’t matter how many lines of code are in the program. Every program written with Xcode requires a project, no matter how small the program is.

This is very important so I will repeat it. Every program written with Xcode requires a project.

How Do I Create an Xcode Project?

Choose File > New Project. A window containing a list of project templates will open. Select one of the templates and click the Next button (or the Choose button in Xcode 3.1). You will be asked to name the project and to choose where to save it on your hard disk.

What Project Template Should I Select?

When you create a new project, you will notice there are a lot of project templates. The following templates are the ones Xcode beginners are most likely to select:

  • People learning C++ normally select the C++ Tool project template.
  • People learning C normally select the Standard Tool project template.
  • People learning Objective C normally select the Foundation Tool project template.
  • People learning Java normally select the Java Tool project template.
  • People learning Mac GUI programming normally select the Cocoa Application project template.

The C++ Tool, Standard Tool, and Foundation Tool project templates are in the Command LIne Utility section. The Java Tool project template is in the Java section. The Cocoa Application project template is in the Application section.

How Do I Compile My Program?

You compile your program in Xcode by building the project. You build your project by using the Build menu or the shortcut buttons in the project window toolbar. There are three options.

  • Build, which compiles your code into a working program.
  • Build and Run, which compiles your code and runs the program in Xcode.
  • Build and Debug, which compiles your code and runs the program in Xcode’s debugger.

If you wrote a command-line program and you’re using Xcode 3, you must open Xcode’s debugger console to see the output of the program. Choose Run > Console to open the console.
|

Xcode 3.1: Setting the Compiler Version

Xcode 3.1 adds the Compiler Version build settings collection, which provides a GUI to set the compiler version for C, C++, and Objective C programs. Prior to the release of Xcode 3.1, there wasn’t a big need to set the compiler version. GCC 4.0, Xcode’s default compiler for Xcode 2 and 3, was the right choice for most people. You had to use GCC 4 to compile for Intel Macs. Unless you were writing an appliction in C++ and wanted to support versions of Mac OS X older than 10.3.9, there wasn’t much of a reason not to use GCC 4.0.

But with the release of Xcode 3.1 comes two new compiler choices: GCC 4.2 and LLVM (Low Level Virtual Machine)-GCC 4.2. Programs compiled with GCC 4.2 and LLVM-GCC 4.2 will not run on anything older than Mac OS X 10.5. If you want to support Mac OS X 10.4, you’ll still have to use GCC 4.0.

Of the two new compiler choices, LLVM-GCC 4.2 is the more intriguing choice. LLVM-GCC 4.2 uses GCC as the front end parser, which does the syntax checking. It uses LLVM for the back end to do optimization and create object code. Code compiled with LLVM-GCC should run faster than code compiled with GCC. Apple is providing a lot of support for the LLVM project so don’t be surprised if LLVM becomes a big part of Mac development in the future. You can learn more about LLVM-GCC by reading the release notes.
|

Xcode 3.1 Addition: Adding Libraries to Target

Xcode 3.1 added a feature that simplifies adding dynamic libraries to an Xcode project. When you open a target’s information panel and click the General tab, the information panel shows the frameworks and libraries that will be linked to the target when you build it.

To add a library or framework to the target, click the + button. A sheet opens with a list of all the frameworks, dynamic libraries, and object files for the active SDK you’re using. Select the libraries and frameworks you want to add, and click the Add button.

If you want to add a third party library or framework that’s not part of the SDK you’re using, click the Add Other. An Open File dialog box will open, which you will use to navigate to the location where the library or framework resides.
|

Installing Xcode in Non-Default Location

When you install Xcode the default install location is the Developer folder on your startup disk.

/Developer

Xcode 3.x gives you the option of installing Xcode wherever you want. This feature comes in handy if you want to have two versions of Xcode installed on your Mac. To install Xcode in a different location, choose a custom install. You will see a column titled Location with a pop-up button cell with the value Developer. Click the button and choose Other. You will be prompted for a location to install Xcode.

If you choose to install Xcode in a location other than /Developer, make sure the path to your install location contains no spaces. If there are spaces in the path, you can get hangs and crashes in the Xcode Tools. I made the mistake of installing Xcode 3.1 into a folder titled Xcode 3.1. Interface Builder 3.1 would hang and give me the spinning beach ball with certain nib files. I couldn’t figure out what was causing the hang until I read on Apple’s Xcode mailing list about Interface Builder crashing when there were spaces in the installation path. I changed my folder name from Xcode 3.1 to Xcode3-1, and the Interface Builder hangs went away.

Hopefully this post will save someone the pain and aggravation I went through.
|

Xcode Book Status Report

It’s WWDC time, and many of you are wondering how things are progressing on the new edition of Xcode Tools Sensei. I’ve made a lot of progress on the Xcode material. I’ve decided to break up the huge Xcode chapter (Chapter 1 in the current version of the book) into smaller chapters. I currently have the Xcode material divided into the following chapters:

  • Projects
  • Source Code Editing
  • Modeling Tools
  • Building Projects
  • Debugging
  • Version Control

The chapters with the most remaining work are the Interface Builder and Instruments chapters. I was vaugely dissatisfied with the original Interface Builder chapter. I am looking at ways to improve the material for Interface Builder 3.

The Instruments chapter has been the biggest problem for me. Instruments is huge. I could write an entire book just on Instruments. Plus in Xcode 3, ObjectAlloc and Sampler are Instruments trace templates instead of separate applications so I have to merge material from the ObjectAlloc and Sampler chapters into an Instruments chapter. It’s a tough chapter to organize.
|

New Blog Home

I’m sure you’ve noticed the blog now looks like the rest of the website. That’s because I’ve moved the blog from Blogger to my domain. Besides the new look, the switch will let me have blog categories so you can read only the posts that are interesting to you. The categories are currently irrelevant as I’m just starting with the new location, but the categories will eventually be useful.

I would like to eventually move the more interesting posts from the old blog to the new one, but for now, I’ve placed a link to the old blog on the site map. The link to the old blog appears only on the blog page. I didn’t want to litter the whole site with links to an old blog.
|