Qt for Windows - 从源代码构建

此页面描述了配置和构建的过程,针对 Qt for Windows . To download and install a pre-built Qt for Windows, follow the instructions on the Qt 快速入门 页面。

Step 1: Getting the Sources

Qt sources can be installed in the Qt Online Installer. Source packages are also available as Git repositories , as archives in the Qt 帐户 (commercial users), and on download.qt.io (open-source users).

If you install the sources through the Qt Online Installer, they will be available in the Qt installation directory, for instance C:\Qt\6.5.1\Src .

If you downloaded the source archive, extract it to a directory of your choice, for instance C:\dev\Qt\src .

注意: The path to the source directory must not contain any spaces or Windows specific file system characters. The path should also be kept short. This avoids issues with too long file paths in the compilation phase.

Step 2: Install Build Requirements

To build Qt from sources you need a build environment with a supported compiler and various build tools available:

构建工具

工具 支持版本 描述
CMake Version 3.16 and newer (3.17 and newer for -debug-and-release builds 3.21 and newer for -static builds). Required for configuring the Qt build. Available in the Qt Online Installer and on cmake.org .
Ninja - Recommended tool for building Qt. Available in the Qt Online Installer and on ninja-build.org .
Python Version 3 Required build tool. Windows installers are available on python.org , or from the Microsoft Store .

The executables cmake.exe , ninja.exe ,和 python.exe must be available in your build environment. You achieve this by adding the respective directory to your PATH 环境变量。

编译器

The following compilers and configurations are supported in Qt 6.5:

操作系统 体系结构 编译器
Windows 10 (1809 或更高版本) x86_64 MSVC 2022, MSVC 2019, MinGW 11.2
Windows 11 x86_64 MSVC 2022, MSVC 2019, MinGW 11.2
ARM 中的 Windows arm64 MSVC 2019/2022 技术预览

注意: 仅支持将 ARM 中的 Windows 作为部署目标。应用程序必须自 x86-64 的 Windows 机器交叉编译并部署到目标。

QDoc 依赖

QDoc uses Clang to parse C++ code. If you wish to build QDoc manually, refer to 为 QDoc 安装 Clang 对于特定构建要求。

步骤 3:设置环境变量

我们推荐创建桌面链接打开命令提示,采用的环境设置类似 Command Prompt 菜单条目提供通过 Visual Studio。这是通过创建应用程序链接完成,传递 .cmd 文件设置环境和命令行选项 /k (保持打开) 给 cmd.exe .

假定文件名为 qt6vars.cmd 和 Qt 文件夹名为 C:\Qt\6.5.1\Src :

REM Set up Microsoft Visual Studio 2019, where <arch> is amd64, x86, etc.
CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat" <arch>
SET _ROOT=C:\Qt\6.5.1\Src
SET PATH=%_ROOT%;%PATH%
SET _ROOT=
					

然后,可以创建桌面链接通过指定命令 %SystemRoot%\system32\cmd.exe /E:ON /V:ON /k C:\Qt\qt6vars.cmd as application.

Depending on your individual setup, you might also need to make the installation directories of CMake, Ninja, and Python part of the SET %PATH% line above.

注意: Setups for MinGW are similar; they differ only in that the bin folder of the installation should be added to the path instead of calling the Visual Studio setup script. For MinGW, please make sure that no sh.exe 可以在路径中被找到,因为它影响 mingw32-make .

步骤 4:构建 Qt 库

To configure the Qt library for your machine type, run the configure.bat script in the source directory. Alternatively, if you have already completed Step 3, you may double-click on the desktop link you created, and type configure and start configuring the Qt library in your selected directory.

By default, Qt is configured for installation in the C:\Program Files\Qt directory, but this can be changed by using the -prefix 选项。

The 配置选项 page contains more information about the configure options. See Qt for Windows - 图形加速 for specific options regarding graphics acceleration.