Introduction to testing C++ code: Setup

To complete this course you need a computer with access to the internet. All attendees should follow the steps below to install the necessary tools and download the exercises before the session.

Installing tools

Firstly, you need to install a C++ compiler if you do not have one already. It should support the C++14 standard (which all modern compilers do). You also need CMake, at least version 3.11.

You will need a text editor or IDE to work with the example code. For Linux and macOS users, Visual Studio Code is a good option, but for Windows users we strongly recommend that you use Visual Studio Community, which includes a compiler, CMake and an IDE.

Nowadays, many popular IDEs, including Visual Studio and Visual Studio Code, provide built-in support for CMake and CTest and will automatically configure your project if a CMakeLists.txt file is discovered in its root. For Visual Studio Code, you will need the CMake extension installed.

Windows: Installing Visual Studio Community

Windows users can download Visual Studio Community here. When installing it, make sure you select the relevant options for C++ development.

macOS: Installing developer tools and CMake

Apple provide a suite of UNIX-style command line tools that include a version of the clang compiler. Install them by opening the “Terminal” app and running:

$ xcode-select --install
xcode-select: note: install requested for command line developer tools
This will open dialog that asks for your confirmation to install the tools. If it does not open a dialog, it may be because it is already installed (the error message will be clear).

You can check that the tools are correctly installed by running:

$ clang --version
Apple clang version 12.0.0 (clang-1200.0.32.27)
Target: x86_64-apple-darwin20.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

If your output is similar, then you have installed the developer tools correctly.

If you are developing on macOS, it is easiest to install CMake via Homebrew. If you do not have Homebrew installed, please follow these installation instructions.

Then you can install CMake like so:

brew install cmake

Linux: Installing gcc and CMake

If you are using Ubuntu Linux, you can install the gcc compiler and CMake with your package manager:

sudo apt install build-essential cmake

Downloading the exercises

The exercises are available in a GitHub repository here. If you are familiar with git, follow the link to the repository and clone it locally. Alternatively, download the zip file directly and extract it somewhere.

Testing that your setup works

Lastly, it is very important that you check that all of these tools have been installed correctly on your system before the session starts.

We will now check that the example code builds correctly on your machine. Start by opening a terminal and navigating to wherever you extracted the exercise code before. Note that if you are doing this on Windows, it is very important that you use Developer Powershell (or Developer Command Prompt) as your terminal, otherwise you won’t be able to use CMake.

Once you have done that, you can configure the project with CMake like so:

cmake -B build

This will generate some files in a new folder, build.

Then you can build the code by running:

cmake --build build/

If this completes successfully, then congratulations! You have a working development machine.