![]() |
||||
|
This guide is meant for students at Leiden University who want to do their Programmeermethoden work on a Mac.
A default Mac OS X installation doesn't include development tools like the g++ compiler. In order to compile programs on your Mac, you need to install the Xcode toolkit as described in this guide.
Note: This guide was written by Sjoerd Henstra (thanks, Sjoerd!) in 2009 for OS X 10.5 (Leopard) and updated for OS X 10.6 (Snow Leopard). OS X 10.7 (Lion) and new versions of Xcode have since been released. These instruction may no longer apply.
The easiest way to install Xcode is from your Mac OS X install disk. Insert the disk and look for XcodeTools.mpkg. Its precise location may be different for each version of OS X. The Leopard upgrade DVD has a folder Optional Installs, which contains a folder Xcode Tools which contains the installer.
If you can't find Xcode on your OS X disk or you can't find your OS X disk itself, you can also download Xcode. Go to the Mac Developer Program and apply for a free ADC Online Membership. Shortly after filling out the form, you'll get a confirmation e-mail.
Now go to the ADC Member Site, log in and go to the downloads section. Select Developer Tools on the right side of the page and scroll down to a version of Xcode that will work on your system. If you don't know which version of OS X you're running, click the Apple icon on your menu bar and click About This Mac.
When you open the downloaded disk image, you'll find XcodeTools.mpkg inside.
Once you've located the Xcode installer (XcodeTools.mpkg), double click it. The following instructions were written for Xcode 3.1, but the installer should be roughly the same for all versions.
Installation may take several minutes.
When you write a C++ program, you're just writing plain text, which a compiler turns into an executable. Therefore you can use any plain text editor to write your programs. However, some text editors have special features that make them more suitable for code editing.
The most notable feature is syntax highlighting, which allows programs to color words and characters based on their meaning. For example, comments can be colored grey, numbers red, special keywords blue, etc. Other useful features are automatic indentation and tab expansion (turning tabs into spaces).
I will briefly discuss several free editors for Mac OS X here.
Note that the easiest way to launch an application is usually through Spotlight. Press command+space to open Spotlight. Start typing the name of the application and it should show up in the list. Highlight the application and press return to launch it.
Now that you have all your tools, it's time to test them.
Use your code editor of choice to write the following:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
Save this file as hello.cc on your desktop (~/Desktop).
Open a terminal (command+space to open Spotlight, search for Terminal and press return to launch).
Now go to the Desktop directory, then compile and run the program as you would under Linux.
cd Desktop g++ -Wall -o hello hello.cc ./hello
Now you can write, compile and run C++ programs on your Mac. Be sure to always thoroughly test your programs on the Linux machines at LIACS as well.