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:
does not work on a mac, and there is no easy way around it.
system("PAUSE");
Secondly, you may have noticed that the example program used the command:
instead of just
std::cout<<"Hello World";
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.
42 comments:
Hi,
I found your post quite useful, I'm a new Mac user and I need to program in C++ for my school projects; I was just about to give up and restart with windows to use visual or dev, but now I think I'II stay on Leopard.
Thanks
By the way, I heard something about a library called "Ncurses" that has functions like system("PAUSE")
Hi,
I also found your post useful. I'm new to Mac and doing my grad study abroad. I was used to Windows at my home country. Know in Canada I should get used to Mac. I found it somehow confusing at first to write C++ program and compile them using the terminal. But now thanks to your post I can use Xcode.
Thanks a lot.
Hi,
Thanks a lot. I'm a new Mac user and your post really helped.
Hi,
I'm a new mac user, and decided I wanted to start learning C++ and being a beginner at both makes the process intimidating but your post was clear and perfect. I just programmed my first program! thanks a ton!
nicky
Thank you for actually showing how its done! I've been a newbie at this Mac OS, and now I have Xcode to work, after an embarrassingly long time learning how to actually compile a program. Thanks again.
I did this, but no window pops up when I double click on the hello excecutable. Is there some other software I need to install?
Thanks!
hi,
I am a new Mac user and I am using the MAC os x 10.4 and XCode 1.5. I guess same steps described in the post are used for the XCode version available with me. But when I build my project the Xcode quits and displays a message
"The application XCode quits unexpectedly"
Can U please tell me which went wrong?
I have not made any changes in my program and it is a simple hello world program
Thanx
Umesh P
Hey thanks, i found this post really useful.
Hi,
Thanks for the posting, I now can create a simple C++ program with Xcode! I was diddling with application and that got me nowhere. Your advice was right on!
For system("PAUSE"), if the idea is to give you some time to see the console or to do something, you may want to consider sleep(). e.g. calling sleep(15) would effectively pause for 15 seconds.
Hey your post was super useful, I was just wondering though...I downloaded the newest version of Xcode (3.1), and it said on the site it was compatible with Mac OSX 10.5. I successfully downloaded it, but when I open the MPKG file it says it cannot be opened. I tried manually going in and opening it with the installer, but that doesn't work either. Any suggestions? You can message me at special_shi at hotmail dot com, thanks in advance!
Very nice little tutorial. I've been meaning to play with XCode for a while, but it's a bit intimidating and feels like overkill for doing simple CLI development. This has definitely encouraged me to give it a try.
hii...
it was of immense help dude...thanks a lot...i m also new to mac and was previously working on linux...u saved my day...
cheers...
sweet.. I love simplicity, you probably saved me many hours.
GOD BLESS YOU, MAY YOU WIN A FREE MACBOOK AIR AND A 16 GIG IPHONE!! YOU HAVE SAVED MY DAY!!
Neil,
Since you're describing creation of a console application it might be important to know how to feed parameters into it. I'm trying to run it from Terminal but no success so far. I've just added the following to the code"
if (argc != 2)
{
cout << "usage: " << argv[0] << " [file_name]" << endl;
return 1;
}
thank you
Thank you very much!!
Your post really helped me!
thank you
Hi
If you are going to write simple console command line c++ programs you don't need to use XCode's "complicated" interface.
1. Write your C++ source code using simple TextEdit and save as your_file_name.cpp
2. Open a terminal window. (Applications/Utilities/Terminal)
3. Go to your source code folder and type g++ your_file_name.cpp -o your_file_name
4. Type ./your_file_name to run your file.
Hope it helps...
Birkan Ovayolu
So i am using a book that tells me to make the "hello world.cpp" then compile that to a "hello world.obj" then link it to "any needed libraries to produce an executable program". any help with that. the book is "Sams Teach Yourself C++ in 21 days". yea its gona take me longer than 21 days. also i should mention i am on Xcode 2.5.
Thank you so much for the post! I am teaching myself to program on my MBP and was just about to give up on XCode. You are my hero of the day!
Thanks alot. I'm new to mac, too. And I am gonna
need c++ for my programming classes.
Thank you!!! Saved me heaps of time d%!#ing around. I hope you find help for your stuff! You deserve it. regards, Jamie
hey Neil,
thanx a lot man....I have been working for 3 days on writing a C++ code in xcode and ur post really helped.
Thank you oh so very very much. I desperately need this!
Hi Neil !!
Your post was really helpful for me. But am wondering if you could tell me one more thing. I have some user defined header files and am using them in my cpp code. where do I need to place these header files so that my code can use them ?
I will search for it meanwhile :)..
Thanks.
hey,
Your guide was great man it really helped. I did just have one question, I havent been able to use string at all to declare any words...and I ran the same thing on Dev C++ Bloodshed and it ran right through, but on Xcode its not letting me get a word from the user. is there something special I need to know?
-mike
Hi, many thanks, I followed your instructions and it worked.
Incidently, in terminal, at the prompt, "c++ filename" also seems to work.
Thanks for your post!
Thank you so much, I've been trying to figure that out for months!
Really a very use ful post. Thanks a lot for the help.
These instructions are good enough that they still apply to XCode 3.2 - at least enough to figure the rest out. Shameful that there's not a simple tutorial built in to XCode.
Thanks for the post! Very helpful!
Thanxs a million
u were of great help buddy
thank u brother
u were f gr8 help
thanx alot
tanks alot
Great post! What smooth intro to C++ on Mac:-) /Lasse
So happy I found this page. Thank you very much!
you saved my life!!!!
In just five minutes I was able to understand how it works after hours of nothing! Thanks a lot!
Thanks man this helped a lot. I guess I'm a noob for not realizing that the c++ tool would be located in the command line tool drop down... haha.
God Bless You!!! I was going into a whirl spin trying to figure Xcode out, until I read your post. Thank you very much.
Hi Neil,
Your blog regarding Xcode C++ tutorial for Mac users is simply amazing. Its quite useful for a Mac newbie. I just bought the MacBook Pro yesterday and was wondering how to code simple C++ programs. Your tutorial saved my day. Thanks a lot for the tutorial. Cheers!!
Thanks for the post Neil. After more than two years, the post still stands helpful.
And for those who couldnt see a pop up terminal window after clicking on the executable file... try command+shift+R
Post a Comment