Clang 10 in a Docker container for C++17 and C++20 development
Posted on December 14, 2017 by Paul
Updated 19 April 2020
If you want to try the latest stable version of Clang in a Docker container, you are in the right place. Running Clang in a container has the advantage that it is light on resources and won’t mess with your underlying OS. The last point is especially important if your host operating system is macOS, on which it is a really bad idea to directly install a binary Clang other than the one that comes with Xcode. I’ve tested the approach presented in this article on Windows 10, macOS Mojave and Ubuntu Linux.
I assume that you have Docker installed on your machine, if not go to the Docker website and install it. After the installation, open a Terminal or, if you are on Windows, a PowerShell window and check if Docker was properly installed with:
This is a snippet of what I see on a macOS machine:
Next, we are going to use a Dockerfile that will prepare an Ubuntu image with the latest Clang:
You can get the last version of the above Dockerfile from my GitHub (Hint: use the Download button if you don’t have a Git client on your machine):
In order to build an up do date image, you can use (make sure you are in the same folder as the Dockerfile):
the above will create a new image named ubuntu_clang_image. Once the process is finished, you can check the list of available images with:
Now, we need to create a container from ubuntu_clang_image. First, we’ll create a folder that will be shared between our host OS and the Docker container, this will let us use a our preferred C++ text editor (or IDE) from our host:
If your host OS is Windows 7, 8 or 10, you need to do an extra step. Make sure that you shared your drives with Docker (this will let you share a local Windows folder with a Docker container). For example, if you want to share drive C, open the Docker’s Settings panel check the drive and press Apply:
Next, we’ll create a container named ubuntu_clang from the above image:
Please note that you need to run the above command only once!
At this point, you should be at a Bash prompt in your container, e.g. this is what I see on my machine:
Check the version of Clang with:
you should see something like this:
Now, from your host machine, go to clang_tests and create a new file named if_test.cpp, with the next C++ code:
The file should be visible from the container too, cd to clang_tests and check what files are present, this is what I see:
You can compile and run the above C++ program with:
This is what I see on my container:
Next, let’s try to compile a program that uses the C++17 Filesystem:
Save the above file as fs_test.cpp and compile it with:
This is what I see on my container if I run the above code (you should see a list of files that are present in the folder where you have the executable):
Finally, let’s test if we can use C++17 std::optional:
This is what I see if I build and run the above code:
Clang 10 has partial support for the new C++20 standard, you can use std=c++20 in order to enforce the C++20 standard. Here is an example of using the new C++20 std::span:
This is what I see if I build and run the above code:
If you want to leave the container, just write exit and you should be at your default shell prompt, e.g.:
Next time when you want to start the container, you can do it with:
If you don’t remember the name of your Docker container, use the next command to list all available containers:
If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.