How To Run C++?

There are many ways to compile and run C++ programs. For this course, we'll be running text-based standard C++ programs, so there should be no requirement for any particular platform. You need to be able to do two things:

  1. Edit, compile and run C++ code, using any tools you like and can get working.
  2. You need to be able to compile and run programs from the command line, which is needed occasionally. It also helps to understand how things work.

Below are several ways to do all this. All the software mentioned here is free to use.

Run Linux

C was developed to run Unix, and there is a long historical connection. One of best ways to use C++ is in its home, on the Unix clone known and Linux. Linux is a free OS having many distributors, each of which gives a slightly different flavor to their version. Any Linux distro will have command-line C++ compiler and some variety of editors and IDEs, though you may have to install them from the distro repository. A repository is sort of like an app store, but less pretentious.

There are many ways to run Linux:

  1. You can install one of the many free Linux distributions, such as Fedora (my favorite), Ubuntu, Arch, or many others. These can be installed and used on a USB jump drive without disturbing your existing OS.
  2. If you run Windows, you can use the Windows Subsystem for Linux, which basically runs a Linux system under Windows. Here are some official instructions, and some others, for setting that up.
  3. You can run Linux on a virtual machine. If you don't have a Virtual Machine Manager handy, you can install Virtual Box. This site has VM images you download and run once the VMM is installed. (Some newer or premium versions of Windows come with hyper-V, but it cannot be added if it's not already there.)

Run On Mac

I don't have access to a Mac, so you are going to be pretty much on your own with this. Here are a two randomly Googled sets of instructions that may be of some use. You can Google additional random links as needed. Both of the given ones involve installing X-code, which apparently includes a full development environment, including the ability to compile and run from the Terminal app, as also needed. The XCode home is here, which might help you get started with that.

If you survived 116 or 216, you might have jGRASP installed. Once you have a C++ compiler, jGRASP should be able to find it and support C++, if you want to stick with something you know.

Visual Studio is an IDE that includes a Mac version. I have not used it, but many people do.

The Mac kernel is a Unix derivative. Historically, Mac is more closely related to Unix than Linux is. But that's a long and fairly irrelvant story.

Run on Windows

Command-Line Environment

The MSYS2 package comes with the compiler, and provides a command-line environment and Unix tools, including commands to to compile, edit and run C and C++ programs. You might also install (on Windows) a decent programmer's text editor, such as Notepad++. That's all that's really required.

Development Environments

If you prefer the GUI sugar, a simple one is Code::Blocks. A single install provides an IDE and compiler. You will see several versions; make sure to select the the one for mingw-setup, which provides the compiler and an install script that gets things set up nicely.

CodeBlocks does not have a current Mac version, though they do link some old ones. Not sure you want that.

You might have jGRASP installed as a hangover from 116 or 216. It can compile C++ programs, if it can find the compiler. Once you install a compiler and add it to the command path (see below), jGRASP will magically be able to compile and run c++ programs. If jGRASP is your thing.

Visual Studio is a popular IDE, though I have not worked with it. I don't know if it comes with a compiler, though you can always get MinGW-64. VS is also available for Mac.

Compiling and Running From the Command Line

You need to be able to compile and run from a command line. If you go the MSYS route, this is the only way you have to run things. An IDE installation may solve this problem for you, or it may not. It might add the compiler to the Windows path, so you can compile from the Windows shell, or it might have a control to open a properly-configured command window.

One that does not help at all is Code::Blocks. A simple solution, which will probably work for you, is to download this Windows shortcut. A Windows shortcut is really just a small file that holds some configuration information, but Windows will probably not let you just download one (we must pretend a shortcut is special, you see). So this one is wrapped in a zip file. When you click the link, you should be able to see the single-file archive and and drag ccmd.lnk to your desktop. Once there, when you click it, it will open a Windows command shell which knows where CodeBlocks hides its compiler, so g++ and related commands will work.

You can build and compile a program with CodeBlocks, then run it from the shell, but you will need to find it first. For this, you must note where you stored the project, which will become a folder, and look for the executable file there. Change to its directory with the cd command. It's all lots of fun.

The more general solution is to add the compiler to the command path yourself, then any command window (include powershell) will be able to perform compiles. You will also need to this if you want to use jGRASP. Here is the procedure. Part of the problem is knowing where the compiler actually is. The path named in the linked instructions (step 5) is for using a program called NodeJS, which is not C++. For CodeBlocks, the location is generally C:\Program Files\CodeBlocks\MinGW\bin.MSYS will probably use something like C:\MinGW\bin. You should be able to find the location using the Windows find from the task bar. Look for the file c++.exe, and use the path of the directory (folder) where it resides.

No matter which of these approaches you use, you will using one of two compiler packages. Linux uses the GNU Compiler Collection (GCC), which has the gcc plain C compiler and g++ C++ compiler. MinGW, which can be installed by itself or as part of MSYS or CodeBlocks is a Windows port of GCC. This name is derived from “Minimal GNU Windows”. (Minimal is probably meant to contrast with other projects intended to implement a large part of the Unix environment under Windows.) GNU certainly runs on Mac, but the semi-official compiler set for Mac is LLVM, with the Clang C/C++ compiler. It's usually installed to work like GCC, using the same command syntax, to keep users happy and existing development environments from breaking.

GCC can be installed on Mac. LLVM can run Linux, and presumably Windows (though I've never looked it up), so these associations are not fixed, but you must never underestimate the Power Of The Default. There are other compilers, especially for plain C, but GCC is still the big kid on the block, so it's not going away. LLVM is newer, and well-regarded as having taken some of the right lessons from its predecessors, so it's not going anywhere, either. C++ is a beastly difficult language to compile. None of which makes much difference for an introductory C++ course.