MTH4300 Home

Preparing Mac Computers for C++ Programming

1. Dual view

Dual view is a setup where:

Remark. On Mac, the File Explorer equivalent is the app called Finder.

2. Dual View on Mac

2.1 Finder and its customization

2.1.1 Opening the Finder

The first step is to open the Finder. Locate the Finder in the Dock and click on it (the "Dock" is the line at the bottom of the screen).

Screenshot of a Mac computer showing an open Finder window and the Dock at the bottom of the screen.

The instructions in subsections 2.1.2 and 2.1.3 need to be performed only once, and your computer will then have the "Macintosh HD" icon in the easily accessible Favorites menu of the finder app.

2.1.2 Navigating to the hard disk, if it is hidden

While Finder is active, press the button command on the keyboard. Keep the command pressed with one hand, and then press the button up \(\uparrow\) repeatedly until you end in the root of you computer. At this point you will see an icon to the hard disk.

Screenshot of an open Finder window positioned at the root folder, with the hard disk icon visible.

2.1.3 Making the shortcut to the hard disk

Visually locate the section called Favorites in the left menu of the Finder window. Drag the hard disk icon to the top of the section favorites. The link "Macintosh HD" should now appear in the Favorites menu, as shown in the picture below.

Screenshot of an open Finder window positioned at the root folder, with the hard disk also visible in the sidebar.

2.1.4 Navigating to the home folder

You can close Finder and re-open again to verify whether "Macintosh HD" is in the favorites menu. If it is, then click on it and navigate to your home folder. Most likely, this is the sequence of folders that you need to go through: Macintosh HD, Users, [your username]. In this example, the username was programmer, and the final outcome is shown in the picture below.

Screenshot of a reopened Finder window with the hard disk visible in the sidebar.

Your folder display may involve big icons. The menu on top of the Finder window allows you to switch the view. Switch it to the option called "as List". Most serious programmers need this list view and not the default view which is called "as Icons".

2.2 Terminal and its customization

2.2.1 Add Terminal to the Dock

By default, the Terminal app does not appear in the Dock. For convenience, many programmers prefer to keep it there for quick access.

To add Terminal to the Dock:

Screenshot of a Finder window during the search for the Terminal app.

2.2.2 Open the Terminal

Move your mouse to the Dock and click on the Terminal app.

Screenshot of the Dock showing the Terminal app.

2.3 Creation of folder

Give the command mkdir ma4300 to create a folder that we will use to write our programs. Although the command was given to the terminal, you will notice that the folder has appeared in the Finder as well.

Screenshot of a Mac computer with a Finder window open in the background, the Terminal app in focus, and the command 'mkdir ma4300' typed in the Terminal.

2.4 Entrance to the folder

Enter the newly created folder both in Terminal and in the Finder. Type the command cd ma4300 in the Terminal. Double click on the folder ma4300 in Finder.

Screenshot of a Mac computer with a Finder window in the background showing the ma4300 folder and the Terminal app in focus in the same folder.

2.5 Source file creation

Create the source file. Type the command echo > prog1.cpp in the Terminal. You will notice a new file that appears in the Finder. The name of the file is prog1.cpp

Screenshot showing the Finder window in the background with the newly created file prog1.cpp after running the command 'echo > prog1.cpp'.

2.6 Typing of the source code

Move the mouse over to the Finder window. Right-click on the file prog1.cpp and choose to open the file with some simple text editor. Type the C++ source code as shown in the picture below. Make sure you save the file after the c++ source was typed in.

Screenshot showing a TextEdit window whose title bar displays the file name 'prog1.cpp', with source code typed in the file.

2.7 Compiling the source code

Compile the source and create the binary by typing the following command in the Terminal:

c++ prog1.cpp -o binary01 
A new binary file must appear in Finder. If it did not, then there are two possible errors. The first possible error is that your computer does not have a c++ compiler. The second possibility is that there are errors and/or typos in your source file which caused the compiler to refuse to make the binary. For example, you typed the word int with capital I instead of lowercase i. Or, you typed long with capital L instead of lower-case L. C++ is case-sensitive. In either of the cases, the terminal has given you an error message that you need to investigate.

Screenshot showing TextEdit and Finder windows in the background and the Terminal in focus, after the command 'c++ prog1.cpp -o binary01' was executed; the file binary01 appears in Finder alongside prog1.cpp.

2.8 Executing the binary

Execute the binary by typing ./binary01. The program should print 70 in the terminal.

Screenshot showing TextEdit and Finder windows in the background and the Terminal in focus, after the command './binary01' was executed; the output '70' appears in the Terminal.