Tuesday, February 26, 2008

My Beloved Wiener Dog





Thursday, February 14, 2008

How To: Compile and Run Basic C++ Programs in Xcode

This tutorial is to help out anyone that wants to create a terminal based c++ program using Xcode for Mac OS X. This is mainly for the people in CS 150 at ODU, but I think many can benefit from this as a getting started guide. If you know some basic C++ already this guide will be easier to you, but no prior knowledge is required for this how-to.

1. Download Xcode

The program that Mac users need to develop their programs is called Xcode. Xcode is an IDE that is built specifically for OS X users. Other IDEs that you may be familiar with are Visual Studio, and Dev-C++ both of which are for Windows. To download Xcode go here and click the link on the right labeled "Tools Downloads". Next, you will need to select a version of Xcode that is supported by your version of OS X. 10.4 Tiger users should use Xcode 2.5 and 10.5 Leopard users should use Xcode 3.0. If you have not done so already you will need to register for Apple Developer Connection site in order to download (registration is free). This is a somewhat large download, so grab a coffee or something and then come back to step 2.

2. Installing Xcode

Now that we have downloaded Xcode we will need to install it. The file that you downloaded should be a DMG file. Double click the file to mount the disk image, and a new icon on your desktop should appear. Double click the new icon, and then double click the Xcode installer which should be called something like xcodetools.mpkg. Go through the options like you were installing a normal piece of software, and then when it is all done click finished. Hooray! Xcode is installed.

3. Starting a New Project

This is where it gets interesting. When I first started to try Xcode out this is where I got confused and hopefully you will not go through what I did. I am using Xcode 3.0 with Leopard, so if you are a Tiger user things may be a bit different. First, you will want to open Xcode. The Xcode application is not located in your applications folder, but in the Developer directory. Its location is Macintosh HD/Developer/Applications/Xcode. Double click the Xcode icon to open the application, and you will be greeted by a startup assistant. I just went through with the default options, but if you are picky about where you projects are saved etc. then feel free to change the options. When the welcome window pops up just close it by clicking the "x" at the top left of the window. Then, go to File in the menu bar and then select "New Project". Now the intuitive thing to do would be to select the "Application" drop down, but for us we actually want to select the "Command Line Utility" drop down item. From there you should select "C++ Tool" and click next. Give your project a name and specify a place to save your project. I will call mine "hello" and leave the Project Directory as it is. Now click finish and you project window should appear. Now we will start the coding of our project.

4. Coding Compiling and Running

One of the files in the window should jump out at you right away and that is main.cpp. Double click it to open it in the editor. As you can see it has a very simple Hello World application in it, and this is where you will put your project's code. Now lets compile and run our Hello World program. Click on the build button at the top left of the editor window. If it asks you to save just click save all and it will compile. Now go to your "Project Organizer" window and then double click the executable file which in this case is just "hello" without any file extension. A terminal window should now open and your program should run. Now you know how to program in C++ on a Mac!

5. Notes

There are a few things about coding in C++ on a Mac that are different than a PC. For example the command:


system("PAUSE");
does not work on a mac, and there is no easy way around it.

Secondly, you may have noticed that the example program used the command:

std::cout<<"Hello World";
instead of just
cout<<"Hello World";
this is because the example is not using the std namespace. So you can code in 2 different ways. Either option A:

#include <iostream>
using namespace std;
int main (int argc, char * const argv[]) {

     // insert code here...
     cout << "Hello, World!\n";
     return 0;
}

Or option B:

#include <iostream>

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

     // insert code here...
     std::cout << "Hello, World!\n";
     return 0;
}


Note that the "\n" after Hello World is an indicator to start a new line after Hello World.