簡體   English   中英

"通過 Github Actions 構建時如何使用 clang-10 或 gcc-10?"

[英]How to use clang-10 or gcc-10 when building via Github Actions?

我正在用 C++ 編寫一個庫,它實現了一些不同的協程原語,並且該庫針對新發布的 C++20。 因此,它還利用了諸如在 C++20 中添加到語言中的概念之類的東西。

我想使用 github 操作來構建庫,但構建失敗,因為ubuntu-latest<\/code>使用 GCC 9 和 CLang 9,但我的庫至少需要 GCC 10 或 Clang 10 才能構建。

我嘗試通過設置-DCMAKE_CXX_COMPILER=g++-10<\/code>來配置構建操作,但在配置 CMake 階段該操作失敗,因為在系統上找不到 g++-10。

有什么方法可以指定 github 操作應該使用 GCC 10 還是 Clang 10?<\/strong>

這是我嘗試運行的最新工作流文件:

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

正如一些程序員所提到的,從 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 時,同樣的解決方案也適用。

我將 gcc-9 和 clang-10 用於C (only) 項目

- 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++ 項目更新更多的替代品,只是例子。

您可以通過訪問https://github.com/actions/virtual-environments查看已安裝的內容。

如果你在 2022 年嘗試這個, 現在ubuntu-latest有“GNU C++ 9.3.0, 10.3.0”。 g++鏈接到版本 9,但g++-10在 PATH 上可用,無需任何額外的安裝步驟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM