'How to use clang-10 or gcc-10 when building via Github Actions?

I'm writing a library in C++ that implements a few different coroutine primitives, and the library is targeted at the newly released C++20. As a result, it also makes use of things like concepts that were added to the language in C++20.

I want to use github actions to build the library, but builds are failing because ubuntu-latest uses GCC 9 and CLang 9, but my library requires at least GCC 10 or Clang 10 to build.

I attempted to configure the build action by setting -DCMAKE_CXX_COMPILER=g++-10, but the action fails in the Configure CMake phase because g++-10 can't be found on the system.

Is there any way specify github actions should use GCC 10 or Clang 10?

This is the most recent workflow file I tried running:

name: CMake

on: [push]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    # The CMake configure and build commands are platform agnostic and should work equally
    # well on Windows or Mac.  You can convert this to a matrix build if you need
    # cross-platform coverage.
    # See: https://docs.github.com/en/actions/configuring-and-managing-workflows/configuring-a-workflow#configuring-a-build-matrix
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Create Build Environment
      # Some projects don't allow in-source building, so create a separate build directory
      # We'll use this as our working directory for all subsequent commands
      run: cmake -E make_directory ${{runner.workspace}}/build

    - name: Configure CMake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash
      working-directory: ${{runner.workspace}}/build
      # Note the current convention is to use the -S and -B options here to specify source 
      # and build directories, but this is only available with CMake 3.13 and higher.  
      # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
      run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=g++-10

    - name: Build
      working-directory: ${{runner.workspace}}/build
      shell: bash
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: cmake --build . --config $BUILD_TYPE

    - name: Test
      working-directory: ${{runner.workspace}}/build
      shell: bash
      # Execute tests defined by the CMake configuration.  
      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
      run: ctest -C $BUILD_TYPE

And this is the point where it fails:

Run cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=g++-10
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
  The CMAKE_CXX_COMPILER:

    g++-10

  is not a full path and was not found in the PATH.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.


-- Configuring incomplete, errors occurred!
See also "/home/runner/work/conduit/build/CMakeFiles/CMakeOutput.log".
See also "/home/runner/work/conduit/build/CMakeFiles/CMakeError.log".
##[error]Process completed with exit code 1.


Solution 1:[1]

As Some programmer dude mentioned, installing g++ from apt is the way to go (unless it's installed by default); adds a minute or two to the build. Then you can tell cmake which compiler it should use by passing CC and CXX variables during configure step:

- run:   |
         sudo apt update
         sudo apt install gcc-10 g++-10
  shell: bash

# ... #

- run:   cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
  shell: bash
  env:
   CC:   gcc-10
   CXX:  g++-10

Same solution applies when you want to use clang.

Solution 2:[2]

You can see what's installed by visiting https://github.com/actions/virtual-environments.

If you're trying this in 2022, now ubuntu-latest has "GNU C++ 9.3.0, 10.3.0". g++ is linked to version 9, but g++-10 is available on the PATH without any extra installation steps.

Solution 3:[3]

I am using both gcc-9 and clang-10 for C (only) project.

- name: Setup dependencies
  if: startsWith(matrix.os, 'ubuntu')
  run: |
    sudo apt-get install -y gcc-9 llvm-10 clang-10
    sudo update-alternatives \
      --install /usr/bin/gcc gcc /usr/bin/gcc-9 100 \
      --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-9 \
      --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9 \
      --slave /usr/bin/gcov gcov /usr/bin/gcov-9
    sudo update-alternatives \
      --install /usr/bin/llvm-ar llvm-ar /usr/bin/llvm-ar-10 100 \
      --slave /usr/bin/llvm-ranlib llvm-ranlib /usr/bin/llvm-ranlib-10 \
      --slave /usr/bin/llvm-cov llvm-cov /usr/bin/llvm-cov-10
    sudo update-alternatives \
      --install /usr/bin/clang clang /usr/bin/clang-10 100

PS you need to update more alternatives for C++ project, just example.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Carl Walsh
Solution 3 puchu