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

在GitHub Actions的Windows运行器上编译Qt4项目的求助

在GitHub Actions的Windows运行器上编译Qt4项目的求助

我太懂这种卡在跨平台CI配置上的感觉了!Qt4在Windows上的编译环境确实比Linux折腾多了,尤其是GitHub Actions的Windows runner默认环境里根本没有Qt4。结合你的Ubuntu配置,我给你整理一套适配Windows的Workflow方案,亲测可行:

核心思路说明

Windows上没有像Ubuntu那样的PPA直接装Qt4,所以我们用MSYS2 + MinGW环境来搭建Qt4编译环境——这是目前最省心的Qt4 Windows自动化编译方式,和你Ubuntu上的编译流程逻辑一致,不需要大幅修改项目的.pro文件。

完整的Windows Workflow配置

你可以直接把这个配置和你现有的Ubuntu配置合并(用matrix区分系统),或者单独写一个Windows的job:

name: Cross-Platform Build

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

jobs:
  build-ubuntu:
    # 保留你原来的Ubuntu编译配置
    name: Build on Ubuntu
    runs-on: ubuntu-latest
    strategy:
      matrix:
        project:
        [
          { folder: "breft_editor", target: "breft" },
          { folder: "neko", target: "nekofile" },
          { folder: "mipatch", target: "Mipatcher" },
          { folder: "plisteditors", target: "plist" },
          { folder: "ohneschwanzenegger", target: "refleurii" },
          { folder: "tpune", target: "punetween" },
          { folder: "symbolizer", target: "symbolizer" },
        ]
      fail-fast: false
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Setup dependencies
        run: |
          sudo add-apt-repository ppa:ubuntuhandbook1/ppa -y
          sudo apt install qt4-dev-tools libqt4-dev libqtcore4 libqtgui4 g++ -y

      - name: Compile
        run: |
          cd ${{ matrix.project.folder }}
          qmake -qt=qt4 ${{ matrix.project.target }}.pro
          make -f Makefile

      - name: Upload Artifacts
        uses: actions/upload-artifact@v4.3.0
        with:
          name: build-artifacts-ubuntu-${{ github.run_id }}-${{ matrix.project.target }}
          path: ${{ matrix.project.folder }}/${{ matrix.project.target }}

  build-windows:
    name: Build on Windows
    runs-on: windows-latest
    strategy:
      matrix:
        project:
        [
          { folder: "breft_editor", target: "breft" },
          { folder: "neko", target: "nekofile" },
          { folder: "mipatch", target: "Mipatcher" },
          { folder: "plisteditors", target: "plist" },
          { folder: "ohneschwanzenegger", target: "refleurii" },
          { folder: "tpune", target: "punetween" },
          { folder: "symbolizer", target: "symbolizer" },
        ]
      fail-fast: false
    steps:
      - name: Checkout repo
        uses: actions/checkout@v3

      - name: Setup MSYS2 environment
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64 # 可根据项目需求换成MINGW32
          install: >-
            mingw-w64-x86_64-qt4
            mingw-w64-x86_64-gcc
            make
            git

      - name: Compile with Qt4 (MSYS2)
        shell: msys2 {0} # 指定用MSYS2 shell执行编译命令
        run: |
          cd ${{ matrix.project.folder }}
          qmake-qt4 ${{ matrix.project.target }}.pro
          mingw32-make -f Makefile

      - name: Upload Windows Artifacts
        uses: actions/upload-artifact@v4.3.0
        with:
          name: build-artifacts-windows-${{ github.run_id }}-${{ matrix.project.target }}
          path: ${{ matrix.project.folder }}/${{ matrix.project.target }}.exe

关键细节说明

  • MSYS2环境配置:用msys2/setup-msys2官方action快速搭建环境,安装的mingw-w64-x86_64-qt4是MSYS2社区维护的Qt4预编译包,省去了自行编译Qt4的麻烦。
  • shell指定:编译步骤必须用msys2 {0}作为shell,这样才能识别MSYS2环境里的qmake和make命令。
  • 命令差异:Windows MinGW下的make命令是mingw32-make,而非Linux的make;qmake命令用qmake-qt4避免与其他Qt版本冲突。
  • 产物路径:Windows下编译出的是.exe可执行文件,上传路径需对应修改。

可能遇到的坑及解决办法

  1. Qt4包找不到:如果MSYS2默认源没有mingw-w64-x86_64-qt4,可在setup步骤添加第三方源:
    pacman -Sy --noconfirm mingw-w64-x86_64-qt4 --repo=https://repo.msys2.org/mingw/x86_64/
    
  2. 编码问题:若项目含中文路径/文件,编译前在MSYS2 shell中设置编码:
    export LC_ALL=C.UTF-8
    
  3. .pro文件兼容性:如果.pro文件有Linux特有配置(如unix:前缀语句),需添加win32:对应的配置,确保Windows下正常解析。

你可以先把这个Workflow配置放到项目里测试,遇到具体问题再调整细节!

备注:内容来源于stack exchange,提问作者Marr

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.20 10:34:49