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

CMake项目在GitHub Actions中Windows可构建,Linux构建失败求助

问题分析与解决方案

核心问题定位

从错误日志来看,两个关键问题:

  1. OpenCV 4的Release版本在GitHub Actions的Ubuntu环境下编译失败
  2. 后续CMake配置报错CMAKE_MAKE_PROGRAM is not set,这是OpenCV构建失败导致的连锁反应

结合本地Ubuntu能正常构建的情况,问题大概率出在GitHub Actions环境的配置缺失,以及项目CMake脚本中的硬编码路径问题。


具体修复步骤

1. 移除CMakeLists中硬编码的Windows路径

你的CMakeLists里手动添加了Windows平台的vcpkg头文件路径,这在Linux环境下完全无效,还会干扰依赖查找:

# 移除这一行
${CMAKE_CURRENT_SOURCE_DIR}/build/vcpkg_installed/x64-windows/include

vcpkg会通过CMAKE_TOOLCHAIN_FILE自动管理所有依赖的头文件和库路径,不需要手动指定平台特定路径。

2. 完善Linux CI环境的编译依赖

GitHub Actions的ubuntu-latest默认缺少OpenCV编译所需的部分系统依赖,在构建前添加以下步骤安装:

- name: Install System Dependencies
  run: |
    sudo apt-get update
    sudo apt-get install -y build-essential make gcc g++ libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

3. 统一Linux和Windows的vcpkg初始化流程

你的Windows CI手动克隆并初始化vcpkg,但Linux使用setup.sh,可能setup.sh中的vcpkg配置不完整。建议Linux也采用和Windows一致的初始化步骤,确保环境一致:

# 替换原有的Run Setup步骤
- name: Setup vcpkg
  run: |
    git clone https://github.com/microsoft/vcpkg.git
    cd vcpkg
    ./bootstrap-vcpkg.sh
    ./vcpkg integrate install
    cd ..

4. 显式配置CMake构建参数

在Linux的Configure步骤中,明确指定构建类型和生成器,避免自动检测问题:

- name: Configure and Build Project
  run: |
    cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Release -Wno-dev
    cmake --build build --config Release -j2

这里用-j2减少并行编译线程数,避免GitHub Actions runner内存耗尽导致编译失败(OpenCV编译Release版本内存占用较高)。


完整的Linux CI Job示例

build-linux:
  name: Build Project (Linux)
  runs-on: ubuntu-latest
  env:
    VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
  steps:
    - name: Export GitHub Actions cache environment variables
      uses: actions/github-script@v7
      with:
        script: |
          core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
          core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

    - name: Checkout
      uses: actions/checkout@v4.1.7

    - name: Install System Dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y build-essential make gcc g++ libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

    - name: Setup vcpkg
      run: |
        git clone https://github.com/microsoft/vcpkg.git
        cd vcpkg
        ./bootstrap-vcpkg.sh
        ./vcpkg integrate install
        cd ..

    - name: Configure and Build Project
      run: |
        cmake -S . -B ./build -DCMAKE_BUILD_TYPE=Release -Wno-dev
        cmake --build build --config Release -j2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 18:34:50