Installing a specific version of c++ compiler

Installing a specific version of c++ compiler

Installing a specific version of c++ compiler might be required in some cases. In the basic scenarios, it is enough to use your versions of compilers and build the system delivered in your distribution package system. For more complex cases we need to update them manually. It took me a while to figure out what is the easiest way to install the newest version of compilers and build system as their home pages were not crystal clear in that matter. Hence I want to share with you my experience and the easiest way to achieve it – at least in my opinion.

Prerequisites

I will base my configuration on a previous post – C++ environment setup, which is fundamental for my all posts. [1] So in this post, we will only slightly modify the Dockerfile to download the newest versions of needed tools. The examples will be shown mainly on compilers but the same steps can be performed on other libraries and programs. So you can take the basic image from the previous post and add your own tools.

For the moment of writing this article (01.27.2023), those are the newest available packages. Those steps probably won’t change that much or won’t change at all in the nearest future. In case, you will need a newer version, please just adjust the release numbers from the given tool Github repository.

Installing the specific GCC version

In short, in order to install the newest GCC version – for the moment of writing this blog it is 12.2.0 – we need to paste the following lines of code into the base Dockerfile from the previous post. The apt-get command will download some packages needed for the manual installation.

The second RUN command will fetch the release version from the GitHub page and untar it. The next step – calling ./contrib/download_prerequisites is very specific for GCC. It is facilitation by the maintainers so we don’t have manually install every dependency package. This simple script will do it for us. More about this approach can be read here. [2]

RUN apt-get install -y flex \
  && apt-get install -y g++-multilib \
  && apt-get install -y bison bisonc++

RUN cd /home && wget https://github.com/gcc-mirror/gcc/archive/refs/tags/releases/gcc-12.2.0.tar.gz \
  && tar -xzvf gcc-12.2.0.tar.gz \
  && rm -rf gcc-12.2.0.tar.gz \
  && cd gcc-releases-gcc-12.2.0 \
  && ./contrib/download_prerequisites \
  && cd .. \
  && mkdir objdir \
  && cd objdir \
  && $PWD/../gcc-releases-gcc-12.2.0/configure --enable-languages=c,c++ --enable-multilib \
  && make -j $(nproc) \
  && make install -j $(nproc) \
  && ldconfig \
  && cd .. \
  && rm -rf objdir \
  && rm -rf gcc-releases-gcc-12.2.0

The next steps are pretty common. We do configure, make and install. One strongly recommended step is to create a build directory – objdir – outside of the source files so we can rebuild the GCC once we need it.

It is worth mentioning that GCC will take a while to compile. On my machine, with 16 cores it took approximately an hour to compile. So please be patient and grab your coffee.

Installing the specific CLANG version

In the case of the clang compiler and other clang-related tools, the approach is very similar and also takes approximately the same time.

In contrast to the GCC clang it uses CMake instead of make. So the whole process seems to be easier and more modern. Most of the steps are very common for CMake projects. We proceed with the configuration, build, and installation steps.

The one thing that is worth focusing on is -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld". This option is used to specify what tools we want to compile. In case we want everything we should pass "all" to that variable.

In the following command, we have reduced the number of available processors to half to reduce the chance of overflowing the RAM. So please adjust the “-j” parameter value to your machine. As we reduced the number we can expect around 2 hours of compiling.

RUN cd /home && git clone https://github.com/llvm/llvm-project.git \
  && cd llvm-project \
  && cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" \
  -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
  && cd build \
  && ninja -j $(($(nproc)/2)) \
  && ninja install \
  && cd ../../ \
  && rm -rf llvm-project

Installing the specific CMake version

As a small bonus in this article we install the newest version of CMake to have the benefits of the newest features that build systems can offer.

The whole installation is pretty fast and straightforward. Just copy-paste the following Docker commands inside the Dockerfile from the previous C++ environment setup post.

All it does is install needed package dependencies and use CMake to install itself. It should be a matter of minutes which is a nice contrast to the compiler’s installation.

RUN apt-get install -y openssl \
  libssl-dev

RUN cd /home && git clone https://github.com/Kitware/CMake.git \
  && cd CMake \
  && ./bootstrap \
  && make -j $(nproc) \
  && make install -j $(nproc) \
  && ldconfig \
  && cd .. \
  && rm -rf CMake

Brief Summary

In order to apply changes from this article you need to either add the code snippets to your base Dockerfile from the previous article and rebuild the image or you can create a completely new image. In both cases, you will need to create a new container but don’t worry if you mount your workspace as I mentioned previously you won’t lose any personal data.

The whole Dockerfile can be found on my GitHub repository. [3]

In case you don’t have time or a powerful enough machine to compile those projects but have relatively fast ethernet you can download the ready image from my docker hub. [4]

References

1 thought on “Installing a specific version of c++ compiler”

  1. Pingback: C++ Environment setup - Quality Coders

Leave a Comment

Your email address will not be published. Required fields are marked *