C++20 and the GCC Compiler – Cloudywizzard

C++20 and the GCC Compiler

The latest C++ standard C++20 introduces a lot of new language features, unfortunately at the moment the GNU Compiler Collection (gcc) has no support for several of these new features (like the long awaited modules for example).

To use these new feature you’ll need to install the latest snapshot of the gcc-11 compiler. You will need to compile the compiler manually since it’s probably not available in your distro’s repository.

For Debian/GNU Linux and derived distro’s you can follow these instructions

First install the prerequisites with APT: sudo apt install libgmp-dev libmpfr-dev libmpc-dev flex bison gawk byacc elfutils gdc texi2html apt-file debtags cmake build-essential

Next we download the latest snapshot and extract the archive and compile it (this can take some time). If the mirror you use to download the archive also offers a sha512.sum file you should also download that one, so you can verify the download.

sha512sum -c sha512.sum --ignore-missing
tar xf gcc-11-20210404.tar.xz
cd gcc-11-20210404/
mkdir build
cd build
../configure --prefix=/opt/gcc-11 --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-11
make -j 8
sudo make install
sudo ln -s /opt/gcc-11/lib64/libstdc++.so.6.0.29 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.29
sudo ldconfig

Adapt the name of the archive to the correct one and adapt the argument to make -j to reflect the number of cores/threads you CPU has, this will lower the compile time a lot (for example on a MacBook Air (2014) without this option compilation will take hours.

If you have various compilers installed you can use update-alternatives to quickly switch between compilers (this is handy since the gcc-11 compiler is not released as stable and will crash on occasion).

sudo update-alternatives --install /usr/bin/gcc gcc /opt/gcc-11/bin/gcc 11
sudo update-alternatives --install /usr/bin/g++ g++ /opt/gcc-11/bin/g++ 11

You can switch between compilers with

sudo update-alternatives --config gcc
sudo update-alternatices --config g++

Compiling could be done with the following command: g++ -Wall -Wextra -fmodules-ts -std=gnu++20 -pedantic -static-libstdc++ main.cpp -oProgramName

You can leave the options -Wall -Wextra and -pedantic options from the command (they just give more warnings and error reports).

Leave a Comment

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

Deze website gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.