CSCI 221 Setting Up for C++

For CS1 you probably worked a bit to get things set up on your computer so that you could program in Python. For that course, we normally ask that you install three things

  • A command-line console. This comes for free as the Terminal application on a Mac, and is available in a few forms on Windows including GitBash that’s part of the git for windows tool suite.

  • A Python interpreter. This is normally found at the language system’s site for download.

  • A developer-oriented text editor. While the Mac offers TextEdit and Windows has offered things like Notepad (try Notepad++ maybe), we’ve been encouraging use of programs like SublimeText and Atom. The idea is to start using an editor that does things like syntax highlighting, that is extensible and is smart about several programming languages, and that probably has hooks for complicated programmer actions.

If you didn’t take my version of CS1, you might find it useful to see my Homework 0 for that course. It takes you carefully through installing each of the things listed above.

In any case, if you have the above items on your computer already then you are already quite close to having a decent-enough environment for developing C++ code. What’s missing is that, for C++, you need a compiler to produce runnable programs. A compiler is a tool that takes C++ source code, reads it, and builds a program that can be excecuted directly on your computer. This program that it produces is sometimes called a executable binary file, because it is just low-level machine code that is built to run directly (sometimes the word used is natively) on your specific machine.

Below we give some guidelines for getting a C++ compiler on both the Windows and the Mac system. Note that there are several ways that this can be done on each system. We’ve chosen the method that has worked best for most people, maybe requires the fewest major steps, and is the most explainable.

Note that there now are ways to develop C++ without a compiler. There are several sites on the web that allow you to develop and run small C++ programs. You might find these useful while you are learning the language, but you shouldn’t come to rely on them for completing the work of this course. Also, there are Jupyter notebook extensions that have a C++ interpreter that can run individual statements of the language. As the semester progresses, we might show some of these tools.

Nevertheless, we think it best that you configure your own machine with a C++ compiler. In addition to C++ programming, we’d like to teach you how to use advanced tools for developing and testing C++ programs—including a debugger (gdb or lldb), a memory use analysis tool (like valgrind), and a build tool (e.g.make)—so that you will, in the future, be comfortable using systems other than your own. You’ll want to do a good install now to make things best for you in later weeks.


Using C++ on the Linux dumplings via SSH

We have several Linux computers on campus with all the needed software installed on them, and you will each get an account on some of them. To login to them you just need access to a computer with the Secure SHell (SSH) command line tool (though Putty is an alternative). Open up your computer’s command line and type

ssh REEDNAME@DUMPLING.reed.edu

where REEDNAME is your Reed login name for campus services (mine happens to be jimfix because my Reed email is jimfix@reed.edu; yours will be more like jfix or fixj, or something similar). and DUMPLING is one of our Linux machines, named ravioli, gyoza, empanada, and mandu.

At the moment, only gyoza has been set up for you this way, so you’ll want to use

ssh REEDNAME@gyoza.reed.edu

Once there, you can navigate around with Unix shell commands like

cd: change the folder that you are working within
cd ..: move out one folder
cd ~: move to your user’s "home* folder
ls: list the contents of the current working folder
ls -ltr: elaborated list of the same
pwd: report the current working folder’s path
mkdir FOLDER-NAME: make a new folder within the current working folder
rmdir FOLDER-NAME: delete a folder

You can also inspect and move around files with commands like

cp SOURCE-FILE-PATH DESTINATION-NAME: copy a file
mv OLD-FILE-NAME NEW-FILE-NAME: move a file
rm FILE-NAME: delete a file (i.e. remove)
cat FILE-NAME: output the contents of a file
more FILE-NAME: same, but with screen controls
grep SOME-WORD FILE-NAME: show all lines that contain a word
nano FILE-NAME: edit a file using a “feature-thin” text editor

In addition to the nano editor, you might also try emacs and vim. You can compile any C++ code you write with the command

g++ -o PROGRAM-NAME PROGRAM-NAME.cc

and then run it with the command line

./PROGRAM-NAME

There is also the full suite of git commands for cloning and submitting your work within a Git repository. When you are done with your work, you can leave the dumpling with the command

exit

Getting C++ on Windows

Summary
0. allow Windows subsystems in your system settings (maybe) 1. install the Ubuntu application within the Microsoft Store
2. run the Ubunti app once, create the Linux system user
3. use apt-get to install useful developer packages within Ubuntu
4. link your Windows C: drive to the Linux user’s home

Note: I’ve heard from people that Step 0 may no longer be necessary.


Description
It turns out that Windows now makes it quite easy to obtain similar software to what is installed on the dumpling Linux machines. Within the Microsoft Store there are several versions of “windows subsystems” to run Linux (WSLs) that allow you to run its operating system commands within a console. The instructions below give you the one produced by Ubuntu, the same Linux system as installed on the dumplings.

To install a WSL open up the Microsoft Store and search for Linux. This will bring up several Linux distributions as applications that you can download. Select the Ubuntu one and install it. Alternatively, you can probably just search directly for “Ubuntu” in the store.

Once you’ve downloaded that software application, go ahead and run it. (Should this not work for some reason, see “Changing System Settings…”) Running the Ubuntu WSL brings up a Unix console window, one that will (because it is your first time running it) ask you to name your Linux user and provide their password. I recommend using your Reed login name and password here, mimicking what you would need if this were one of our dumpling machines, but you can choose whatever you like. This username/password combo will be what you type in whenever you rerun this Linux Ubuntu application.

The Linux user you’ve created is the system administrator for this Linux subsystem. There are some configuration commands that you’ll need to run with the command prefix sudo. (This prefix tells the system to “do this as the ‘super user.’”) For that capability, the Linux system will (probably) request that you type the password just to reassure the system that you are the rightful person to make such changes.

You’ll now want to configure the system so that it has the developer software we need. First type the command

sudo apt-get update

This will make sure that your Ubuntu Linux system gets all the latest versions of the packages installed on that subsystem. Having done this, you can now install the necessary software with the command line

sudo apt-get install build-essential emacs gdb valgrind

This installs those four Linux packages (after the word install) that you’ll need. The first includes, for example, g++, git, and vim. The other three (apparently) aren’t considered essential by the Ubuntu people, but you’ll want them as well.

Now you should be able to work here just as you did on the dumplings. Furthermore, you can actually have access to the files you work on through the normal Windows system. Any file you save within your Windows system on your main drive (usually C:) is available under the Ubuntu Linux path /mnt/c/Users/WINDOWS-NAME where WINDOWS-NAME is your Windows user’s name. To make this work easy, you can create a “symbolic link” to that Windows folder within the Linux subsystem. For example, if you’ve created a folder named CS2 on your Windows system’s Desktop you can then enter these command

cd ~
ln -s /mnt/c/Users/WINDOWS-NAME/Desktop/CS2 CS2

and that will create a link under your Linux Ubuntu user’s home that is actually instead that Windows folder on your Desktop. So any editor you use (SublimeText, VScode, Atom, Notepad++) can be used to create and edit C++ source files, just as you normally would as a Windows user, and then you can use Linux Ubuntu’s command line to compile and run this code within the Ubuntu app’s command line. You can also, in Ubuntu, use Git commands to manage your submission repos for this class.

Changing System Settings to Allow WSL
Note that in the recent past, you also needed to change your system settings so that the Windows Subsystem for Linux (WSL) is enabled. I’m told that is no longer the case, but I don’t have a Windows system myself to verify this. If for some reason you do, then open the systems settings from the Start menu by clicking in the gear-labelled item. Within the settings Search for the string “turn windows features on.” If you click the matching item to this search, a list will come up with checkboxes. Scroll through for Windows Subsystem for Linux. Check its box on.


References
Microsoft’s guide to WSL
Lifehacker’s guide to WSL which includes links to other guides
Michael Treat’s guide on GitHub


Getting C++ on the Mac

Summary
1. Install Xcode, probably via Apple’s AppStore
2. Download the Command Line Tools, easiest with xcode-select
3. [very optional] install HomeBrew


The Mac OS X operating system makes aspects of getting Unix-like tools needed for this class similarly easy. OS X has, as its underlayer, a flavor of the Unix operating system (like Linux distributions are) and so the system comes with an application named Terminal that, when run, allows you to type the same set of commands that we listed under the Dumplingsguid just above. Furthermore, when you run this application the system will place you within your Mac user’s home folder (typically at /Users/user-name) and so you will have direct access to all your files, including anything you’ve created or modified with a standard, mouse-based editor or integrated development environment.

Even so, you still need to explicitly turn on the development tools in your Mac’s system in order to get the full features we need for this course. These all are provided as part of Xcode, Mac’s free development environment for building Mac applications (and also iOS apps, etc.) Xcode is a bit too feature-rich for our purposes— we aren’t writing production-quality applications as part of this course— but it does happen to provide a C++ compiler, debugging tools, and the Git command line tools, among other useful things.

Xcode has been has been provided for free in various forms since 10.3, though in some cases you had to install it explicitly from a DVD. Most recently, you install it explicitly through the Mac’s App Store. Most importantly, you want to make sure you’ve installed/enabled the Command Line Tools for Xcode. The easiest documented way I’ve found for doing this is to, in Terminal, type the command

xcode-select --install

This does the actual work of downloading additional tools and then putting them on your machine. (Alternatively, on some versions of OS X, you can select a checkbox named either “command line tools” or "Unix development support, depending, to include them with the Xcode installation.) In any case, this will include a C++ compiler, the Git command suite, Terminal-based editors like vim and emacs, and so forth.

Note that you may need to restart the Terminal application to use them, after you’ve performed the installation.

I have a much older Mac laptop so, at the time I installed Xcode, I wanted to be sure I had access to other command-line tools that I was used to having on Linux (that is, I installed things at a time when Apple made things less easy for me). I chose, then, to install HomeBrew which is a Ubuntu-like Unix package manager for the Mac system. You might consider installing this, as well. It’s documentation pages and install scripts seem to work to get you the Unix command line tools, as well. A competing system to HomeBrew, MacPorts also has a useful guide to installing Xcode, etc. See my list just below for reference.

References
MacPorts
WxWidgets guide
HomeBrew instructions