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.