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. We use the free developer tools supplied by Apple Computer.

Installing g++

  1. Open a Terminal window.

    Press ⌘ Command+Space, type Terminal in the search field, and press the Return key.

  2. In the Terminal window type g++ and press the Return key. We will see an alert box like this:

    mac g++ popup

  3. Choose Install to get only the command line tools unless you want to learn Xcode.

    Xcode can be installed later from the App Store.

  4. After installation, type g++ in the Terminal, press the Return key, and verify the terminal prints the message, "no input files".
    $ g++
    clang: error: no input files
    
  5. After installation, set your computer to show file extensions in Finder.

As an optional step, register (for free) on the Apple Developer Connection as a basic developer. This gives you access to additional software and articles as well as older versions of Xcode.

Setting Up TextEdit

To save code we need to use a plain text editor like TextEdit. However, we need to set up TextEdit to save files using plain text and not in rich-text format (RTF). Our programs will not compile unless we use plain text. These instructions setup TextEdit to make programming easier.

  1. Open TextEdit.

    Press ⌘ Command+Space, type TextEdit in the search field, and press the Return key.

  2. From the TextEdit menu, select Preferences. We will see a dialog box like this:

    TextEdit preferences select plain text, turn off smart quotes

  3. In the Prefences dialog:
    1. Select Plain text.
    2. Set Window Size Width to 80 characters.
    3. Uncheck both Smart quotes and Smart dashes.

To jump to a specific line in an open document, press ⌘ Command+L to bring up the Select Line tool. Enter the line number and press the Return key.

Installing TextWrangler

Even though we can use TextEdit for developing program source code, other editors like TextWrangler are more convenient because we can see line numbers more easily. When we compile code, all errors are reported by line number. Easily viewing line numbers speeds up the program development process. These instructions describe how to install and set up TextWrangler.

  1. Start the App Store application on your Mac computer.
  2. In the App Store, search for TextWrangler and select it from the search list.
  3. Press the Get button and wait for the installation to complete.
  4. In the App Store, press the Open button.
  5. In TextWrangler, open the Preferences dialog.
    TextWrangler > Preferences > Text Files
    
  6. In the Editor Defaults panel, check the box to Auto-expand tabs.
  7. In the Text Files panel, check the box to Ensure file ends with line break.

To start TextWrangler after installation, press the ⌘ Command+Space, type TextWrangler in the search field, and press the Return key.

Compiling and Running Programs

Use this source code to test that your installation can compile and run programs.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}
  1. Copy the above program into a text editor and save the file as hello.cpp on your Desktop.
  2. Open a Terminal window and type cd Desktop follwed by the Return key to change directories (cd) to the Desktop:
    cd Desktop
  3. To compile, enter the following in the Terminal window follwed by the Return key:
    g++ -Wall -Wextra -Wpedantic -o hello hello.cpp
    

    We see the file hello appear on the desktop under an exec icon.

  4. Run the compiled program by typing the following in the Terminal window followed by the Return key:
    ./hello

<< Back

Last Updated: December 01 2017 @14:41:47