How to Install g++ on the Mac

<< Back

On This Page

Introduction

This document provides instructions for setting up the programming environment we use in CS-11 on the Macintosh computer running OS-X. It uses the free developer tools supplied by Apple Computer.

The development environment that Apple supplies is called Xcode. The Xcode tools are available on the OS-X disk, but are not installed by default. Another option is to can download the Xcode tools from the Developer Connection Xcode page. However, it is better to install from the OS-X disk because you get additional tools that can help you when develop code.

If you need more help installing Xcode than provided in this guide, consult the Developer Connection Xcode page. Or better yet, ask a class mate if they were able to set it up. Also, you can email Steve Hodges for technical support.

Installation Instructions

Install the Developer Tools from the OS-X disk or download and install Xcode from the Developer Connection Xcode page. If you install from the OS-X disk, you should download the latest Xcode tools update from the Developer Connection Xcode page.

In addition, to run graphics programs, you must install the optional X11 package.

As an optional step, you should register (for free) on the Apple Developer Connection web page as a basic developer. This gives you access to additional software and articles.

Compiling and Running Programs

To save code you need to use a plain text editor. For saving code you can use TextEdit. However, you need to set up TextEdit to save files using plain text and not RTF. Your programs will not compile unless you use plain text. For instructions on setting TextEdit to use plain text, see: Mac OS X: How to Set Up TextEdit as an HTML or Plain Text Editor

Here is a simple program that you can use to compile and run as a test of your installation. Save the file as hello.cpp on your Desktop.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

Open a Terminal or X11 terminal window to compile and run programs from the command line. Note that you must use an X11 terminal to run graphics programs. In the terminal window, you can use commands like cd to change directories, ls to list files and pwd to print the working directory.

Now in the Terminal or X11 terminal window, use the cd command to change to the desktop:

cd desktop

To compile, you can enter:

g++ -Wall -W -pedantic -o hello hello.cpp

To run the program you can type:

./hello

Easier Compiling Using an Alias

To save yourself typing, you can create a shorter version of the compile command by using an alias. An alias lets you substitute a single word for a long string of commands. To create an alias named "compile", you would type the following command:

alias compile 'g++ -Wall -W -pedantic -o '

To use the alias, you would then type:

compile hello hello.cpp

Note that the alias will disappear when you logout of your computer. To make the alias permanent, you need to save the alias in a file. Use your favorite text editor to open ~/Library/init/tcsh/aliases.mine and add your alias commands.

<< Back

Last Updated: September 07 2008 @15:34:42