For the linux build you can use the following build step to switch the default gcc to gcc-10.
- name: switch to gcc-10 on linux
if: matrix.configurations.os == "ubuntu-latest"
run: |
sudo apt install gcc-10 g++-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
sudo update-alternatives --set gcc /usr/bin/gcc-10
The first 2 lines in the script should be optional, as gcc-10 is already installed in ubuntu-latest. But it doesn't hurt much to have them there and it might help to make clear what is happening and how to reproduce the build locally. You can check the manpage for update-alternatives if you are interested how this works.
windows-latest uses visual-studio as it's default compiler, but since you only asked about gcc I suppose that it already supports the c++ features you are using?
@wirew0rm posted an excellent answer, which works very well generically. It's useful to note that one is allowed to use sudo on the GitHub CI runners, and so one simply needs to install the desired packages:
sudo apt install gcc-10 g++-10
# or
sudo apt install gcc-11 g++-11
# or, for very recent Clang:
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 12
sudo apt-get install libc++-12-dev libc++abi-12-dev
Then, because I was using CMake, I could set the CXX environment variable instead of using sudo update-alternatives:
echo "CXX=g++-10" >> $GITHUB_ENV
The subsequent CMake configure step would then select g++-10 as the C++ compiler.