【问题标题】:Link fmt library on github-action在 github-action 上链接 fmt 库
【发布时间】:2021-09-05 10:18:16
【问题描述】:

早安,

我有以下pythonconanfile.py

from conans import ConanFile


class GeneticAlgorithmProject(ConanFile):
    # Note: options are copied from CMake boolean options.
    # When turned off, CMake sometimes passes them as empty strings.
    # "some_option_name": ["ON", "OFF", ""]
    options = {
    }
    name = "GeneticAlgorithmProject"
    version = "0.1"
    requires = (
        "fmt/8.0.1",
        "effolkronium-random/1.4.0",
        "range-v3/0.11.0"
    )
    generators = "cmake", "gcc", "txt", "cmake_find_package"

    def requirements(self):
        pass

经典跑步者在CmakeLists.txt 上调用Conan.cmake 文件宏run_conan()

Cmake 在步骤- name: Configure CMake 处配置和构建fmt lib:

-- Conan: Using autogenerated Findrange-v3.cmake
-- Found range-v3: 0.11.0 (found version "0.11.0") 
-- Conan: Using autogenerated Findeffolkronium_random.cmake
-- Found effolkronium_random: 1.4.0 (found version "1.4.0") 
-- Conan: Using autogenerated Findfmt.cmake
-- Found fmt: 8.0.1 (found version "8.0.1") 
-- Library fmt found /home/runner/work/github_actions/github_actions/conan-cache/.conan/data/fmt/8.0.1/_/_/package/2c09c8f84c016041549fcee94e4caae5d89424b6/lib/libfmt.a
-- Found: /home/runner/work/github_actions/github_actions/conan-cache/.conan/data/fmt/8.0.1/_/_/package/2c09c8f84c016041549fcee94e4caae5d89424b6/lib/libfmt.a
-- Configuring done
-- Generating done
-- Build files have been written to: /home/runner/work/github_actions/github_actions/build

但是下一步- name: Buildgithub 找不到-lfmt

Run CC=gcc-11 CXX=g++-11 cmake --build ./build --config Release
[ 50%] Building CXX object CMakeFiles/genetic_algo.dir/main.cpp.o
[100%] Linking CXX executable genetic_algo
/usr/bin/ld: cannot find -lfmt
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/genetic_algo.dir/build.make:97: genetic_algo] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/genetic_algo.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Error: Process completed with exit code 2.

这里是 yml 文件:

name: Test Cmake and Conan

on:
  pull_request:
  push:
    branches:
      - main

env:
  BUILD_TYPE: Release
  # Conan cache environment variables
  CONAN_SYSREQUIRES_MODE: enabled
  CONAN_USER_HOME: "${{ github.workspace }}/conan-cache"
  CONAN_USER_HOME_SHORT: "${{ github.workspace }}/conan-cache/short"

# Use a bash shell so we can use the same syntax for environment variable
# access regardless of the host operating system
defaults:
  run:
    shell: bash

jobs:
  build:

    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
          - {
            name: "Linux GCC 11 Release (C++20, Concepts)", artifact: "Linux.tar.xz",
            os: ubuntu-latest,
            cc: "gcc-11", cxx: "g++-11",
            cxx_standard: 20
          }

    steps:
      - uses: actions/checkout@v2

      - name: Cache
        uses: actions/cache@v2
        env:
          cache-name: cache-conan-modules
        with:
          path: |
            ${{ env.CONAN_USER_HOME }}
            ~/.cache/pip
          key: ${{ runner.os }}-${{ env.BUILD_TYPE }}-${{ hashFiles('CMakeLists.txt') }}-${{ hashFiles('cmake/Conan.cmake') }}

      - name: Create Build Environment
        run: cmake -E make_directory ./build

      - name: Install gcc
        if: startsWith(matrix.config.os,'ubuntu')
        shell: bash
        working-directory: ${{ env.HOME }}
        run: |
          sudo apt-get install -y ${{matrix.config.cc}} ${{matrix.config.cxx}}

      - name: Install conan
        run: |
          pip3 install wheel setuptools
          pip3 install conan --upgrade

      - name: Configure CMake
        run: |
          CC=${{matrix.config.cc}} CXX=${{matrix.config.cxx}} cmake -S . -B ./build -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}

      - name: Build
        run: |
          CC=${{matrix.config.cc}} CXX=${{matrix.config.cxx}} cmake --build ./build --config ${{ env.BUILD_TYPE }}

      - name: Test
        working-directory: ./build
        # Execute tests defined by the CMake configuration.
        # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
        run: |
          CC=${{matrix.config.cc}} CXX=${{matrix.config.cxx}} ctest -C ${{ env.BUILD_TYPE }}

当然,一切都是在本地机器上构建的。

【问题讨论】:

  • 有时,二进制格式的微小差异会使链接器完全忽略该库。就像图书馆的架构错误或其他什么一样。也许 Github 操作中使用的环境或编译器可能有所不同。输出是否包含使用的配置文件?另外,您在消费者 CMakeLists.txt 中使用了什么链接? CONAN_LIBS, CONAN_PKG::fmt?

标签: c++ cmake github-actions conan


【解决方案1】:

链接fmt库的正确方法是

target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt)

我使用简单的fmt 不带前缀,但 github 不喜欢这种方式。

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 2021-04-25
    • 2022-12-03
    • 2014-03-31
    • 2020-09-05
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多