Solarian Programmer

My programming ramblings

Clang 7 in a Docker container for C++17 development

Posted on December 14, 2017 by Sol

Updated 25 September 2018

If you want to try the new C++17, using 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:

1 docker version

This is what I see on a Windows 10 machine:

 1 PS C:\DEV> docker version
 2 Client:
 3  Version:           18.06.1-ce
 4  API version:       1.38
 5  Go version:        go1.10.3
 6  Git commit:        e68fc7a
 7  Built:             Tue Aug 21 17:21:34 2018
 8  OS/Arch:           windows/amd64
 9  Experimental:      false
11 Server:
12  Engine:
13   Version:          18.06.1-ce
14   API version:      1.38 (minimum version 1.12)
15   Go version:       go1.10.3
16   Git commit:       e68fc7a
17   Built:            Tue Aug 21 17:29:02 2018
18   OS/Arch:          linux/amd64
19   Experimental:     false
20 PS C:\DEV>

Next, we are going to use a Dockerfile that will prepare a Ubuntu image with the latest Clang:

 1 # We use Ubuntu 16.04 because there are already build binaries for this version of Ubuntu
 2 # Check or for the latest available binaries
 3 FROM ubuntu:16.04
 5 # Make sure the image is updated, install some prerequisites,
 6 # Download the latest version of Clang (official binary) for Ubuntu
 7 # Extract the archive and add Clang to the PATH
 8 RUN apt update && apt install -y \
 9   xz-utils \
10   build-essential \
11   curl \
12   && rm -rf /var/lib/apt/lists/* \
13   && curl -SL \
14   | tar -xJC . && \
15   mv clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04 clang_7.0.0 && \
16   echo 'export PATH=/clang_7.0.0/bin:$PATH' >> ~/.bashrc && \
17   echo 'export LD_LIBRARY_PATH=/clang_7.0.0/lib:LD_LIBRARY_PATH' >> ~/.bashrc
19 # Start from a Bash prompt
20 CMD [ "/bin/bash" ]

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):

1 git clone
2 cd Clang-in-Docker

In order to build an up do date image, you can use (make sure you are in the same folder as the Dockerfile):

1 docker build -t ubuntu_clang_image .

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:

1 docker image ls

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:

1 cd ~
2 mkdir clang_tests

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:

Sharing drive C with Docker on Windows 10

Next, we’ll create a container named ubuntu_clang from the above image:

1 docker run -it -v $PWD/clang_tests:/clang_tests --name ubuntu_clang ubuntu_clang_image /bin/bash

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:

1 ~ $ docker run -it -v $PWD/clang_tests:/clang_tests --name ubuntu_clang ubuntu_clang_image /bin/bash
2 root@706615db0f9c:/#

Check the version of Clang with:

1 clang --version

you should see something like this:

1 clang version 7.0.0 (tags/RELEASE_700/final)
2 Target: x86_64-unknown-linux-gnu
3 Thread model: posix
4 InstalledDir: /clang_7.0.0/bin
5 root@706615db0f9c:/#

Now, from your host machine, go to clang_tests and create a new file named if_test.cpp, with the next C++ code:

 1 // if block with init-statement, the example is a bit silly, 
 2 // but you can use it to check if your compiler supports C++17:
 3 #include <iostream>
 5 int main() {
 6     // if block with init-statement:
 7     if(int a = 5; a < 8) {
 8         std::cout << "Local variable a is < 8\n";
 9     } else {
10         std::cout << "Local variable a is >= 8\n";
11     }
12     return 0;
13 }

The file should be visible from the container too, cd to clang_tests and check what files are present, this is what I see:

1 root@706615db0f9c:/# cd clang_tests/
2 root@706615db0f9c:/clang_tests# ls
3 if_test.cpp
4 root@706615db0f9c:/clang_tests#

You can compile and run the above C++ program with:

1 clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic if_test.cpp -o if_test
2 ./if_test

This is what I see on my container:

1 root@706615db0f9c:/clang_tests# clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic if_test.cpp -o if_test
2 root@706615db0f9c:/clang_tests# ./if_test
3 Local variable a is < 8
4 root@706615db0f9c:/clang_tests#

Next, let’s try to compile a program that uses the C++17 Filesystem:

1 #include <iostream>
2 #include <filesystem>
4 int main() {
5     for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
6         std::cout << file.path() << '\n';
7     }
8 }

Save the above file as test_fs.cpp and compile it with:

1 clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs

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):

1 root@706615db0f9c:/clang_tests# clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs
2 root@706615db0f9c:/clang_tests# ./test_fs
3 "./test_fs"
4 "./if_test"
5 "./if_test.cpp"
6 "./test_fs.cpp"
7 root@706615db0f9c:/clang_tests#

If you want to leave the container, just write exit and you should be at your default shell prompt, e.g.:

1 root@706615db0f9c:/clang_tests# exit
2 exit
3 ~ $

Next time when you want to start the container, you can do it with:

1 docker start -ia ubuntu_clang

If you don’t remember the name of your Docker container, use the next command to list all available containers:

1 docker ps -a

If you are interested to learn more about modern C++ I would recommend reading A tour of C++ by Bjarne Stroustrup.

or Effective Modern C++ by Scott Meyers.

Show Comments