Preparing Mac Computers for C++ Programming
1. Dual view
Dual view is a setup where:
- 1. A Terminal window is open.
- 2. A File Explorer window is open.
- 3. Both the terminal and the file explorer show the same folder.
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).
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.
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.
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.
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:
- 1. Click the magnifying glass icon (Spotlight) in the menu bar.
- 2. Type “terminal” into the search bar until the Terminal icon appears.
- 3. Drag the Terminal icon from the search results to the Dock.
2.2.2 Open the Terminal
Move your mouse to the Dock and click on 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.
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.
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
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.
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 binary01A 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.
2.8 Executing the binary
Execute the binary by typing ./binary01. The program should print 70 in the terminal.