【问题标题】:How to use clang-10 or gcc-10 when building via Github Actions?通过 Github Actions 构建时如何使用 clang-10 或 gcc-10?
【发布时间】:2022-01-31 09:50:56
【问题描述】:

我正在用 C++ 编写一个库,它实现了一些不同的协程原语,该库针对新发布的 C++20。因此,它还利用了诸如在 C++20 中添加到语言中的概念之类的东西。

我想使用 github 操作来构建库,但构建失败,因为 ubuntu-latest 使用 GCC 9 和 CLang 9,但我的库至少需要 GCC 10 或 Clang 10 才能构建。

我尝试通过设置-DCMAKE_CXX_COMPILER=g++-10 来配置构建操作,但该操作在配置 CMake 阶段失败,因为在系统上找不到 g++-10。

有什么方法可以指定 github 操作应该使用 GCC 10 还是 Clang 10?

这是我尝试运行的最新工作流文件:

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

这就是失败的地方:

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.

【问题讨论】:

  • 在一个稍微相关的说明中,我建议您不要只满足于一个编译器。使用 both 编译器进行测试,因为不同的实现可能会暴露代码中的不同错误(以及识别不同的可能问题并发出不同的警告)。
  • drescherjm,g++-10 不在 GitHub 用于运行 Github Actions 的服务器的 PATH 上。他们有针对不同操作系统/构建环境的选项 Github Actions 可以运行,我想知道是否有可用的构建环境确实有 g++-10 和 clang++ 版本 10。
  • 您可以随时添加一个步骤来安装 GCC-10。 IIRC 它应该可以通过标准的 Ubuntu 存储库获得。
  • 一些程序员老兄,添加安装 gcc 10 的步骤会将测试的运行时间从大约 5 秒增加到几个小时。我不相信我可以从 repo 安装它,因为我在他们的服务器上没有 root。
  • 我不建议你从 source 构建它,而是做一个普通的apt install g++-10。而且这样的系统使用虚拟化来分离你的环境,并且你通常在运行你的命令的特定容器中拥有root访问权限。

标签: c++ github-actions c++20


【解决方案1】:

正如一些程序员花花公子所提到的,从 apt 安装 g++ 是可行的方法(除非它是默认安装的);为构建增加一两分钟。然后你可以通过在配置步骤中传递CCCXX变量来告诉cmake它应该使用哪个编译器:

- 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

当你想使用 clang 时,同样的解决方案也适用。

【讨论】:

    【解决方案2】:

    您可以访问https://github.com/actions/virtual-environments查看已安装的内容。

    如果你在 2022 年尝试这个,now ubuntu-latest 有“GNU C++ 9.3.0, 10.3.0”。 g++ 链接到版本 9,但 g++-10 在 PATH 上可用,无需任何额外的安装步骤。

    【讨论】:

      【解决方案3】:

      我将 gcc-9 和 clang-10 用于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 你需要为 C++ 项目更新更多替代方案,只是示例。

      【讨论】:

        猜你喜欢
        • 2017-02-11
        • 1970-01-01
        • 2021-05-26
        • 2021-04-29
        • 2020-04-26
        • 2020-09-09
        • 2021-04-21
        • 2020-02-09
        • 2020-10-28
        相关资源
        最近更新 更多