Solarian Programmer

My programming ramblings

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

Posted on December 14, 2017 by Paul

Updated 24 March 2019

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 macOS machine:

 1 ~ $ docker version
 2 Client: Docker Engine - Community
 3  Version:           18.09.2
 4  API version:       1.39
 5  Go version:        go1.10.8
 6  Git commit:        6247962
 7  Built:             Sun Feb 10 04:12:39 2019
 8  OS/Arch:           darwin/amd64
 9  Experimental:      false
11 Server: Docker Engine - Community
12  Engine:
13   Version:          18.09.2
14   API version:      1.39 (minimum version 1.12)
15   Go version:       go1.10.6
16   Git commit:       6247962
17   Built:            Sun Feb 10 04:13:06 2019
18   OS/Arch:          linux/amd64
19   Experimental:     false
20 ~ $ 

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

 1 # Check for the latest available binaries
 2 FROM ubuntu:18.04
 4 # Make sure the image is updated, install some prerequisites,
 5 # Download the latest version of Clang (official binary) for Ubuntu
 6 # Extract the archive and add Clang to the PATH
 7 RUN apt-get update && apt-get install -y \
 8   xz-utils \
 9   build-essential \
10   curl \
11   && rm -rf /var/lib/apt/lists/* \
12   && curl -SL \
13   | tar -xJC . && \
14   mv clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04 clang_8.0.0 && \
15   echo 'export PATH=/clang_8.0.0/bin:$PATH' >> ~/.bashrc && \
16   echo 'export LD_LIBRARY_PATH=/clang_8.0.0/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
18 # Start from a Bash prompt
19 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@16474c8d08cd:/#

Check the version of Clang with:

1 clang --version

you should see something like this:

1 root@16474c8d08cd:/# clang --version
2 clang version 8.0.0 (tags/RELEASE_800/final)
3 Target: x86_64-unknown-linux-gnu
4 Thread model: posix
5 InstalledDir: /clang_8.0.0/bin
6 root@16474c8d08cd:/#

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@16474c8d08cd:/# cd clang_tests/
2 root@16474c8d08cd:/clang_tests# ls
3 if_test.cpp
4 root@16474c8d08cd:/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@16474c8d08cd:/clang_tests# clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic if_test.cpp -o if_test
2 root@16474c8d08cd:/clang_tests# ./if_test
3 Local variable a is < 8
4 root@16474c8d08cd:/clang_tests#

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

 1 // C++17 filesystem test.
 2 // you can build the code with:
 3 // clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs
 5 #include <iostream>
 6 #include <filesystem>
 8 int main() {
 9     for(auto &file : std::filesystem::recursive_directory_iterator("./")) {
10         std::cout << file.path() << '\n';
11     }
12 }

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@16474c8d08cd:/clang_tests# clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_fs.cpp -o test_fs -lc++fs
2 root@16474c8d08cd:/clang_tests# ./test_fs
3 "./test_fs"
4 "./if_test"
5 "./if_test.cpp"
6 "./test_fs.cpp"
7 root@16474c8d08cd:/clang_tests#

Finally, let’s test if we can use C++17 std::optional:

 1 // C++17 optional test.
 2 // You can compile the code with:
 3 // clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_optional.cpp -o test_optional
 4 #include <iostream>
 5 #include <optional>
 7 void show_optional_value(const std::optional<int> &o) {
 8     // Print the optional has a value if it is not empty
 9     if(o) {
10         std::cout << "Optional value is: " << *o << '\n';
11     } else {
12         std::cout << "Optional is empty\n";
13     }
14 }
15 int main() {
16     // Create an empty optional
17     std::optional<int> o1;
19     // Show the optional value
20     show_optional_value(o1);
22     // Store a value in the above optional variable
23     o1 = -33;
25     // Show the optional value
26     show_optional_value(o1);
27 }

This is what I see if I build and run the above code:

1 root@16474c8d08cd:/clang_tests# clang++ -std=c++17 -stdlib=libc++ -Wall -pedantic test_optional.cpp -o test_optional
2 root@16474c8d08cd:/clang_tests# ./test_optional
3 Optional is empty
4 Optional value is: -33
5 root@16474c8d08cd:/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@16474c8d08cd:/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