Windows下通过MinGW编译源码安装Qt5

目前Qt在线安装程序已不支持Qt5系列,因为我们需要通过源码安装Qt5及以下版本。

MSVC编译Qt5源码的教程网上已经有很多了,由于本人已经安装了MinGW,因此采用MinGW编译QT源码。

自己编译的话只需要下载mingw-w64编译器和Qt的源码,之前在官方Wiki上看到还需要Perl,实际测试发现没有Perl也可以正常编译。

mingw-w64直接去mingw-w64官网下载就可以了,选择MingW-W64-builds下载。安装时记得架构记得选择x86_64,版本的话选择最新的就可以了,我编译时使用的是8.1.0没有问题。

Qt源码的话也去Qt官网下载,也可以选择镜像源:Index of /qt/archive/qt/5.15/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror

Index of /qt/archive/qt/5.15/5.15.0/single/下下载即可,single目录存储Qt源码,其他版本的Qt源码也是类似目录。
.tar.xz和zip均可。
assets/【Qt】通过 MinGW 编译源码安装Qt5/image-20250105215422889.png

安装环境

  • MinGw-w64
  • Perl version 5.12 or later(据说可以不用,没试)

下载好Qt安装包后直接进行解压.

修改

copy
./configure -prefix 安装位置 -release -opensource -static -static-runtime -no-compile-examples -qt-sql-sqlite -opengl desktop -platform win32-g++

编译过程中如果出现以下问题,则需要修改文件.\qtbase\src\corelib\global\qt_windows.h,详细请参考该链接(https://forum.qt.io/topic/138145/error-redefinition-of-struct-_file_id_info/4)

assets/【Qt】通过 MinGW 编译源码安装Qt5/image-20250105213937794.png

copy
#if defined(Q_CC_MINGW)
// mingw's windows.h does not set _WIN32_WINNT, resulting breaking compilation
#ifndef WINVER
#define WINVER 0x601
#endif
# ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x601
#endif
# ifndef NTDDI_VERSION
#define NTDDI_VERSION 0x06000000
#endif
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
// 修改为
#include <windows.h>
#if defined(Q_CC_MINGW)
// mingw's windows.h does not set _WIN32_WINNT, resulting breaking compilation
#ifndef WINVER
#define WINVER 0x601
#endif
# ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x601
#endif
# ifndef NTDDI_VERSION
#define NTDDI_VERSION 0x06000000
#endif
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif

我们将include <windows.h> 被放置在 Q_CC_MINGW 宏的上方,以便成功传递配置,便能解决这个问题

编译结束后安装:

copy
mingw32-make install
mingw32-make docs
mingw32-make install_docs

安装的过程可能会报错,可以直接把整个目录拷贝到安装目录,Qt程序(qmake等)一般在./qtbase/bin路径下。
assets/【Qt】通过 MinGW 编译源码安装Qt5/image-20250105231450882.png
参考资料:

https://wohlsoft.ru/pgewiki/Building_Qt_on_MinGW-w64

https://bugreports.qt.io/browse/QTBUG-64298

https://forum.qt.io/topic/138145/error-redefinition-of-struct-_file_id_info/4

https://cloud.tencent.com/developer/article/2300081
assets/【Qt】通过 MinGW 编译源码安装Qt5/image-20250105224927198.png

posted @   RunTimeErrors  阅读(47)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
🚀