PM on your Mac

This guide is meant for students at Leiden University who want to do their Programmeermethoden work on a Mac.

Last updated: September 2, 2016

This guide was written for recent Mac OS X systems: 10.9 (Mavericks) and later. If you run an OS release older than 10.9, you really want to consider upgrading if possible.

A default Mac OS X installation doesn't include development tools like a C++ compiler. In order to compile programs on your Mac, you need to manually install the software development tools. Before you start, first check whether these tools might have been already installed:

  1. Open a terminal.
  2. At the prompt, try the command c++ --version.
  3. If this works and the version of the compiler is reported (for example Apple LLVM Version 7.3.0), you're all set. Don't continue to read this guide, but start programming.
  4. If the shell reports "command not found" you need to install the development tools. Read on.

Installing the development utilities

XCode is available for free from the Mac App Store. So, you must have an Apple ID. Open the Mac App Store and search for "XCode". Click Install. This is a 4 GB download, so it will take a while to download and install.

After installing XCode, you want to ensure that the XCode command line utilities have been installed. The exact procedure depends on the version of XCode:

Choosing a code editor

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).

A number of free editors for Mac OS X are now described:

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.

XCode
You just installed XCode because it includes the g++ compiler and other tools you'll need for this course, but it's also a code editor. While it's designed for large projects, you can just as easily use it to edit a single file. Open the file menu and select new file. Choose to create a C++ file, specify a name and location, uncheck "also create filename.h" and click Finish. XCode has syntax highlighting, automatic indentation, tab expansion and several other useful features.
TextWrangler
TextWrangler is a general purpose text editor with special support for many programming languages. It does syntax highlighting and automatic indentation by default, while tab expansion which can be configured in the application preferences.
MacVim
MacVim is a Mac version of Vim with a graphical interface. The GUI makes it a little bit easier to use, but it still takes quite a bit of practice. MacVim has syntax highlighting, smart indentation, tab expansion and many other features, most of which need to be configured through text commands.

Compiling a program

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
c++ -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. The compiler used on Apple Mac IS DIFFERENT from the compiler used on the Linux machines at LIACS. Although this will likely not be a problem, you can never be sure.