You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions中windows-latest编译C++/CMake项目找不到gomp.lib

问题描述

我有一个C++&CMake项目,使用GitHub Actions在Ubuntu 22.04和windows-latest平台进行构建与测试。Ubuntu平台构建成功,但windows-latest平台的链接器报错:

LINK : fatal error LNK1181: cannot open input file 'gomp.lib'

提供的配置代码

GitHub Actions YAML配置

# This starter workflow is for a CMake project running on a single platform. There is a different starter workflow if you need cross-platform coverage.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-multi-platform.yml
name: CMake build and test on Windows

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

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

jobs:
  build:
    name: build and test
    # 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/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set reusable strings
        id: strings
        shell: bash
        run: |
          echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
          echo "bin-output-dir=${{ github.workspace }}/bin" >> "$GITHUB_OUTPUT"

      - name: Configure CMake
        run: >
          cmake -B ${{ steps.strings.outputs.build-output-dir }} -G "Visual Studio 17 2022" -A x64
          -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
          -S ${{ github.workspace }}

      - name: Build
        run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ env.BUILD_TYPE }}

      - name: Test
        working-directory: ${{ steps.strings.outputs.build-output-dir }}
        run: ctest --verbose -C ${{ env.BUILD_TYPE }} -LE "^disabled$" -L WD

根CMakeLists.txt

# Works with 3.17 and tested through 3.29
cmake_minimum_required(VERSION 3.17...3.29)

# Set outputs directories.
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# Project name and a few useful settings. Other commands can pick up the results
project(
        MyProject
        VERSION 0.1
        LANGUAGES CXX)

# Only do these if this is the main project, and not if it is included through add_subdirectory
if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
    # Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here
    set(CMAKE_CXX_STANDARD 17)
    IF (WIN32)
        # set stuff for windows
        set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
    ELSE ()
        set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wfloat-conversion -Wno-deprecated-copy -Wno-maybe-uninitialized -pedantic -O3 -fprofile-arcs -ftest-coverage -fopenmp${CMAKE_CXX_FLAGS}")
    ENDIF ()
    # Let's ensure -std=c++xx instead of -std=g++xx
    set(CMAKE_CXX_EXTENSIONS OFF)

    # Let's nicely support folders in IDEs
    set_property(GLOBAL PROPERTY USE_FOLDERS ON)

    # Testing only available if this is the main app
    # Note this needs to be done in the main CMakeLists
    # since it calls enable_testing, which must be in the
    # main CMakeLists.
    include(CTest)
endif ()

# add dependencies
include(cmake/CPM.cmake)
CPMAddPackage("gh:nlohmann/json@3.11.3")
CPMAddPackage("gh:gabime/spdlog@1.13.0")
CPMAddPackage(
        GITLAB_REPOSITORY libeigen/eigen
        GIT_TAG 3.4
        OPTIONS "EIGEN_BUILD_DOC OFF"
        "EIGEN_BUILD_PKGCONFIG OF"
        "EIGEN_BUILD_DOC OFF"
)

include_directories(
        ${CMAKE_SOURCE_DIR}/${FolderSource} # Find headers from source folder
        ${csv_SOURCE_DIR}/single_include # Issue #139 for this repo
)

# The compiled library code is here
add_subdirectory(src)

# The executable code is here
add_subdirectory(apps)

# Testing only available if this is the main app
# Emergency override MODERN_CMAKE_BUILD_TESTING provided as well
if ((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
        AND BUILD_TESTING)
    add_subdirectory(tests)
endif ()

库的CMake示例

# Optionally glob, but only for CMake 3.12 or later:
set(HEADER_LIST "${WingDesigner_SOURCE_DIR}/include/utils/logging_helper.hpp;${WingDesigner_SOURCE_DIR}/include/utils/path_helper.hpp;${WingDesigner_SOURCE_DIR}/include/utils/csv_helper.hpp")
# Make an automatic library - will be static or dynamic based on user setting
add_library(utils logging_helper.cpp path_helper.cpp csv_helper.cpp ${HEADER_LIST})
# We need this directory, and users of our library will need it too
target_include_directories(utils PUBLIC ../../include)
target_link_libraries(utils PRIVATE spdlog::spdlog Eigen3::Eigen csv gomp)
target_compile_features(utils PUBLIC cxx_std_17)

# IDEs should put the headers in a nice place
source_group(
        TREE "${PROJECT_SOURCE_DIR}/include"
        PREFIX "Header Files"
        FILES ${HEADER_LIST})
解决方案

问题核心是:gomp.lib是GCC编译器的OpenMP库文件,而Windows平台使用的Visual Studio编译器有自己的OpenMP实现,对应库为vcomp.lib。直接硬编码链接gomp会导致Windows下找不到库文件,需要通过CMake的跨平台机制处理OpenMP。

1. 用CMake的FindOpenMP模块统一管理OpenMP

在根CMakeLists中引入FindOpenMP,自动检测平台对应的OpenMP编译选项和链接库,替代手动写平台相关的编译旗标:

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
    set(CMAKE_CXX_STANDARD 17)
    # 启用OpenMP检测,CMake会自动处理不同平台的编译选项和库
    find_package(OpenMP REQUIRED)
    
    # 统一基础编译选项,不再区分平台硬编码-fopenmp
    set(CMAKE_CXX_FLAGS "-Wall ${CMAKE_CXX_FLAGS}")
    if(NOT WIN32)
        # 仅在非Windows平台添加额外编译警告和优化选项
        set(CMAKE_CXX_FLAGS "-Wextra -Wfloat-conversion -Wno-deprecated-copy -Wno-maybe-uninitialized -pedantic -O3 -fprofile-arcs -ftest-coverage ${CMAKE_CXX_FLAGS}")
    endif()
    
    set(CMAKE_CXX_EXTENSIONS OFF)
    set_property(GLOBAL PROPERTY USE_FOLDERS ON)
    include(CTest)
endif ()

2. 修改库的链接配置,替换硬编码的gomp

在库的CMake配置中,用CMake提供的OpenMP::OpenMP_CXX目标替代直接写gomp,这个目标会自动适配当前平台的OpenMP库:

set(HEADER_LIST "${WingDesigner_SOURCE_DIR}/include/utils/logging_helper.hpp;${WingDesigner_SOURCE_DIR}/include/utils/path_helper.hpp;${WingDesigner_SOURCE_DIR}/include/utils/csv_helper.hpp")
add_library(utils logging_helper.cpp path_helper.cpp csv_helper.cpp ${HEADER_LIST})
target_include_directories(utils PUBLIC ../../include)
# 用OpenMP::OpenMP_CXX替代gomp,跨平台自动适配
target_link_libraries(utils PRIVATE spdlog::spdlog Eigen3::Eigen csv OpenMP::OpenMP_CXX)
target_compile_features(utils PUBLIC cxx_std_17)

source_group(
        TREE "${PROJECT_SOURCE_DIR}/include"
        PREFIX "Header Files"
        FILES ${HEADER_LIST})

3. 可选:优化GitHub Actions为矩阵构建

如果需要同时在Ubuntu和Windows上运行构建测试,可以改成矩阵构建,减少重复配置:

name: CMake Cross-Platform Build and Test

on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

env:
  BUILD_TYPE: Release

jobs:
  build:
    name: ${{ matrix.os }} build and test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-22.04, windows-latest]

    steps:
      - uses: actions/checkout@v4

      - name: Set reusable strings
        id: strings
        shell: bash
        run: |
          echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

      - name: Configure CMake (Ubuntu)
        if: matrix.os == 'ubuntu-22.04'
        run: >
          cmake -B ${{ steps.strings.outputs.build-output-dir }}
          -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
          -S ${{ github.workspace }}

      - name: Configure CMake (Windows)
        if: matrix.os == 'windows-latest'
        run: >
          cmake -B ${{ steps.strings.outputs.build-output-dir }} -G "Visual Studio 17 2022" -A x64
          -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }}
          -S ${{ github.workspace }}

      - name: Build
        run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ env.BUILD_TYPE }}

      - name: Test
        working-directory: ${{ steps.strings.outputs.build-output-dir }}
        run: ctest --verbose -C ${{ env.BUILD_TYPE }} -LE "^disabled$" -L WD

内容的提问来源于stack exchange,提问作者skrat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:09:59