Вы находитесь на странице: 1из 5

Kevin Morgan Xcode Basics

Revision 2. June 1, 2008. Page 1

Xcode Basics

Xcode is an IDE1 for Mac OS X. To say the least, Xcode is nice because it
essentially allows you to focus on writing code, not wrestling with your build system (it
has many, many other features that make it nice, though). On many projects, especially
for hobbyist programmers whose knowledge may be primarily of the language and not its
compiler (since many texts do not adequately cover build systems), this lets you easily
write code. You only have to deal with errors you make, and let Xcode worry about
making compiler calls. On simpler projects (like this tutorial) its not going to seem
incredibly useful, but on projects with 25+ files it can help to automate tasks that could
potentially become mundane and error prone.

So start up Xcode and Go to File->New Project:

Next find the Command Line Utilities section and select C++ Tool:

1
Integrated Development Environment
Creative Commons Attribution-Noncommercial-Share Alike 3.0
http://gamulabs.freepgs.com
Kevin Morgan Xcode Basics
Revision 2. June 1, 2008. Page 2

For this basic introduction, we’re going to focus more on Xcode than coding, so
we’re just going to write a simple program to print out the factors of a number given over
standard input. So we’ll call our project printFactors:

When you click finish it brings up the base Xcode screen:

This may look different from what you have setup. I use the All-In-One style
layout, which can be enabled via Xcode preferences:

Creative Commons Attribution-Noncommercial-Share Alike 3.0


http://gamulabs.freepgs.com
Kevin Morgan Xcode Basics
Revision 2. June 1, 2008. Page 3

It lets you have the editor, build screens, and debug environment all in one
window, which is useful when you get a build error because you can have the error in one
pane and your code in the one below it all in one window. You wind up messing around
with moving windows a whole lot less.
To dissect this view a little, on the left you have all your files grouped:

In the Source group we have our main source file main.cpp. Below that we have
our Documentation group, which is where we could work with things like man pages.
Products contains our build target, in this case a binary called printFactors. If you’re
doing a different type of project, like a Cocoa Application, it will contain your .app
bundle (which in turn contains a compiled binary). Below that you can take a look at and
edit how your targets get built with respect to compilation, linkage, copying files, and
other build phases.
Select main.cpp and then click on the Editor button in the top right of the screen
to bring up the embedded editor (you could also double click main.cpp and it will open
in a new window):

There are plenty of tutorials on the web about coding in C++, so I’m not going to
go over the code I wrote here too much, but I will note that the algorithm I use can be
greatly improved. I wrote it this way for ease of understanding not speed:
Creative Commons Attribution-Noncommercial-Share Alike 3.0
http://gamulabs.freepgs.com
Kevin Morgan Xcode Basics
Revision 2. June 1, 2008. Page 4

#include <iostream>

using namespace std;

int main (int argc, char * const argv[]) {


int num;
cout << "Please Enter a Number:" << endl;
cin >> num;

for(int i = 1; i <= num; i++)


{
int testVal = num % i;
if(testVal == 0)
{
cout << i << endl;
}
}
return 0;
}

Now, we hit the Build button and make sure our project successfully compiles:

Creative Commons Attribution-Noncommercial-Share Alike 3.0


http://gamulabs.freepgs.com
Kevin Morgan Xcode Basics
Revision 2. June 1, 2008. Page 5

If it doesn’t, we’d need to debug what’s going on. For now it does compile, though, so
switch over to the build and run screen and hit Run:

We get this screen where we can enter a number and view our output:

So even with this simple program you can see how much easier Xcode can make coding
when you need it. If you didn’t want to use Xcode, you could do the following:
1. Get a text editor to allow you to edit C++ files (I like Smultron2).
2. Write your code.
3. Open up Terminal.
4. Type the lines to first compile and then run our application:
g++ main.cpp –o printFactors –Wall
./printFactors

Xcode makes this whole process of coding->building->testing go somewhat faster if you


have lots of files and don’t want to mess around with coding these types of commands in
Make or another build system.

2
http://smultron.sourceforge.net/
Creative Commons Attribution-Noncommercial-Share Alike 3.0
http://gamulabs.freepgs.com

Вам также может понравиться