How can I use a recent version of GCC in GitHub CI?

1.6k views Asked by At

I have a C++ project on GitHub with which I use GitHub CI. My workflow is configured to run on Linux and Windows through the strategy property:

runs-on: ${{ matrix.os }}
strategy:
  matrix:
    os: [ubuntu-latest, windows-latest]

This has worked well for me so far, with Cmake identifying the compiler as GNU 9.3.0. However, I would now like to use an experimental feature added in GCC 10 in my project, and so my build fails due to the old compiler version.

How can I use a more recent version of GCC from my GitHub CI workflow?

2

There are 2 answers

1
wirew0rm On BEST ANSWER

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?

0
jweightman On

@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.