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

GitHub Actions中CMake工作流构建SDL2项目失败求助

解决GitHub Actions中CMake找不到SDL2的问题

方案1:通过apt安装SDL2开发包(Ubuntu Runner专属)

GitHub Actions的Ubuntu默认环境未预装SDL2开发依赖,直接构建会导致CMake找不到配置文件。只需在工作流中添加依赖安装步骤即可解决:

修改后的完整工作流代码:

name: CMake

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

env:
  BUILD_TYPE: Release

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3

    # 添加SDL2依赖安装步骤
    - name: Install SDL2 Development Libraries
      run: |
        sudo apt-get update
        sudo apt-get install -y libsdl2-dev

    - name: Configure CMake
      run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

    - name: Test
      working-directory: ${{github.workspace}}/build
      run: ctest -C ${{env.BUILD_TYPE}}

说明:Ubuntu软件源中的libsdl2-dev包会自动配置好CMake可识别的路径,无需修改项目的CMakeLists.txt,适合快速适配Ubuntu CI环境。

方案2:用CMake FetchContent拉取SDL2源码(跨平台通用)

如果需要跨平台兼容、控制SDL2版本,或者不想依赖系统包,可以通过CMake的FetchContent模块自动下载并编译SDL2源码,确保本地与CI环境一致:

  1. 修改项目的CMakeLists.txt,在find_package(SDL2)之前添加以下代码:
# 解决CMP0135警告
cmake_policy(SET CMP0135 NEW)

include(FetchContent)
FetchContent_Declare(
  SDL2
  GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
  GIT_TAG release-2.28.4 # 替换为你需要的SDL2版本
  DOWNLOAD_EXTRACT_TIMESTAMP true
)
FetchContent_MakeAvailable(SDL2)
  1. 将原来的链接SDL2的代码替换为:
# 替换成你的项目目标名称
target_link_libraries(your_project_name PRIVATE SDL2::SDL2)

说明:此方法无需在CI中安装任何系统依赖,所有依赖都在构建时自动获取,完全跨平台,同时解决了日志中的CMP0135警告。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:04:55