Qt for macOS - 从源代码构建

要求

Building Qt requires a macOS platform SDK and corresponding toolchain to be installed on the system. You can get this by installing the Xcode, as described in Qt for macOS#Build Environment .

You should always use the exact same Xcode and SDK version as listed in the Qt for macOS#Supported Versions , to ensure that Qt has been tested in the configuration you're building.

In addition to Xcode, you will need to make sure you have the following dependencies in your path PATH .

  • CMake (>=3.16, >= 3.18.4 for Ninja Multi-Config, >= 3.21.1 for static Qt builds or building for Apple platforms)
  • Ninja

If you want to build Qt WebEngine or Qt PDF , you will need the following additional dependencies:

  • Python html5lib
  • Bison, Flex
  • Gperf
  • Node.js version 8 or later (version 12 recommended)

QDoc 依赖

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

Steps for Building

The following instructions describe how to build Qt from the source package. You can download the Qt sources from the 下载 页面。更多信息,拜访 Qt 快速入门 页面。

Step 1: Unpack the Archive

Unpack the archive if you have not done so already. For example, if you have the qt-everywhere-src-6.7.2.tar.xz package, type the following commands at a command line prompt:

cd /tmp
tar xf ~/Downloads/qt-everywhere-src-6.7.2.tar.xz
					

This creates the directory /tmp/qt-everywhere-src-6.7.2 containing the files from the archive.

Step 2: Build the Qt Library

To configure the Qt library for your machine type, create a build directory and change to it. Run the ./configure script in this directory.

mkdir -p ~/dev/qt-build
cd ~/dev/qt-build
/tmp/qt-everywhere-src-6.7.2/configure
					

注意: configure always uses the Ninja generator and build tool if a ninja executable is available. Ninja is cross-platform, feature-rich, performant, and recommended on all platforms. The use of other generators might work but is not officially supported.

By default, Qt is configured for installation in the /usr/local/6.7.2 directory, but this can be changed by using the -prefix 选项。

By default, Qt is built as a framework, but you can built it as a set of dynamic libraries (dylibs) by specifying the -no-framework 选项。

As described in Qt for macOS#Architectures , Qt will build for the architecture of your development machine by default. To configure a universal build of Qt with support for both Intel and Apple Silicon Macs, add the following argument:

./configure -- -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64"
					

注意: A universal build always needs to include the architecture of your development machine, as the process relies on tools that are built and run on your machine as part of the overall build.

Be careful to specify the Intel architecture ("x86_64") first, otherwise Qt may not enable all the x86 functionality.

Qt can also be configured to be built with debugging symbols. This process is described in detail in the 调试技术 文档。

The 配置选项 页面包含有关 configure 选项的更多信息。

To create the library and compile all the examples and tools, type:

cmake --build . --parallel
					

-prefix is outside the build directory, you need to install the library, examples, and tools in the appropriate place. To do this, type:

cmake --install .
					

This command requires that you have administrator access on your machine.

步骤 3:设置环境变量

In order to use Qt, some environment variables need to be extended.

PATH               - to locate qmake, moc and other Qt tools
					

This is done like this:

.profile (if your shell is bash), or .zshenv (for zsh) add the following lines:

PATH=/usr/local/Qt-%VERSION%/bin:$PATH
export PATH
					

.login (in case your shell is csh or tcsh), add the following line:

setenv PATH /usr/local/Qt-%VERSION%/bin:$PATH
					

If you use a different shell, please modify your environment variables accordingly.

Qt is now installed.