Try C++17 with Docker

Sep 15, 2017

Docker offers a comfortable way to try the latest C++ features without installing the compiler on the local machine. With Docker it is possible to launch a container just for compilation. In this article I demonstrate how to use the official GCC container to work with C++17 features. GCC has experimental support for the latest C++ standard C++17.

First and foremost, we need to pull the GCC image from Docker Hub. In this case we explicitely pull the latest version of the container because we want to play with the latest features. Currently, the latest image is equal to version 7.2.0.

$ docker pull gcc:latest

For the sake of clarity we use a small makefile to build an executable. This way we keep the docker invocations on the command line short.

test: main.cpp
gcc -o $@ -lstdc++ -std=c++1z $<

The parameter -std= determines the languages standard. GCC accepts several standards, such as C++98, C++11, and C++14. c++1z stands for the latest standard C++17.

For demonstration purposes we use the following simple program which makes use of two new feature that were introduced with C++17.

Before going through the next steps make sure you have the files ready. This is my working directory:

$ ls
Makefile main.cpp

We can now use the following invocation of docker to compile this program:

$ docker run --rm -v "$PWD":/shared -w /shared gcc:latest make

This launches a new container from the image gcc:latest. The parameter -v "$PWD":/shared bind mounts the current working directory of the host to /shared inside the container. The parameter -w /shared determines the working directory inside the container. Thus, make executes our makefile and compiles our program. Finally, the parameter -rm automatically removes the container when it exits.

After docker exits there should be a new executable file called test in the current directory.