使用CMake构建/在命令行上构建项目

本文介绍了如何在命令行环境下使用CMake构建Qt项目,包括设置CMAKE_PREFIX_PATH变量,选择CMake生成器如Ninja,以及进行交叉编译。qt-cmake脚本简化了配置过程,可以自动处理Qt安装路径。对于交叉编译,需要目标平台的Qt安装和相应的工具链文件。
摘要由CSDN通过智能技术生成

Building projects on the command line

在命令行上构建项目

To build a Qt project, CMake needs to know where the Qt installation is located. Usually this is done by setting the CMake variable CMAKE_PREFIX_PATH to Qt's installation prefix. If you are cross-compiling, see Cross-compiling for details on additional variables you will need to set.

​要构建Qt项目,CMake需要知道Qt安装的位置。通常通过将CMake变量CMake_PREFIX_PATH设置为Qt的安装前缀来实现。如果是交叉编译,请参阅交叉编译,以了解需要设置的其他变量的详细信息。

If Qt is installed using the online installer, choose a Qt version within the top-level installation directory. For example, the following command shows how this is done on Windows:

如果使用在线安装程序安装了Qt,请在顶级安装目录中选择一个Qt版本。例如,以下命令显示了如何在Windows上执行此操作:

cmake -DCMAKE_PREFIX_PATH=C:\Qt\6.4.0\msvc2019_64 -S <source-dir> -B <build-dir>

The <source-dir> and <build-dir> placeholders represent the source and build directories of your project.

<source dir>和<build dir>占位符代表项目的源目录和生成目录。

CMake generators

CMake生成器

CMake generates the necessary build system files that enable build tools such as GNU Make or Ninja to build your project.

CMake生成必要的构建系统文件,使GNU Make或Ninja等构建工具能够构建您的项目。

CMake's default generator depends on the platform and build environment. For example on Windows, CMake generates Visual Studio project files if a Visual Studio environment is detected.

CMake的默认生成器取决于平台和构建环境。例如,在Windows上,如果检测到Visual Studio环境,CMake会生成Visual Studio项目文件。

For a consistent developer experience on all platforms, use the Ninja or Ninja Multi-Config generator.

要在所有平台上获得一致的开发人员体验,请使用NinjaNinja Multi-Config生成器。

You can select the CMake generator either by setting the CMAKE_GENERATOR environment variable or using the -G argument:

可以通过设置CMAKE_GENERATOR环境变量或使用-G参数来选择CMake生成器:

cmake -G Ninja ...

qt-cmake

The qt-cmake script is a convenient alternative to configure your project. It eliminates the need for you to specify the CMAKE_PREFIX_PATH. You can find it located in the bin directory of your Qt installation prefix. The script passes all parameters to CMake, so you can use it just like you would cmake\:

qt-cmake脚本是配置项目的方便替代方案。这样就不需要指定CMAKE_PREFIX_PATH。您可以在Qt安装前缀的bin目录中找到它。脚本会将所有参数传递给CMake,因此您可以像使用CMake一样使用它:

C:\Qt\6.4.0\msvc2019_64\bin\qt-cmake -G Ninja -S <source-dir> -B <build-dir>

After the build system files are generated, your project is ready to be built:

生成生成系统文件后,即可生成项目:

cd <build-dir>
ninja

You can also use the generator-independent CMake command:

也可以使用独立于生成器的CMake命令:

cmake --build <build-dir>

Cross-compiling

交叉编译

Building your project for a platform that is different from your development machine is called cross-compiling. An example is building for Android (the target platform) on a Windows machine (the host platform).

为不同于开发机器的平台构建项目称为交叉编译。例如,在Windows机器(主机平台)上构建Android(目标平台)。

Cross-compiling with CMake requires a toolchain file for most platforms. It also requires a Qt version for the development host, in addition to a Qt version for the target platform. For example, you need Qt for Windows and Qt for Android installed to cross-compile for Android on Windows.

​对于大多数平台,使用CMake交叉编译需要工具链文件。除了目标平台的Qt版本外,开发主机还需要Qt版本。例如,您需要安装Qt for Windows和Qt for Android,以便在Windows上交叉编译Android。

Use qt-cmake from the Qt installation for the target platform, to cross-compile your project for that platform:

使用目标平台的qt安装中的qt-cmake交叉编译该平台的项目:

<target-qt>/bin/qt-cmake -S <source-dir> -B <build-dir>

This will configure your project for the target platform. The toolchain file is automatically passed, and possibly other platform-specific variables are set up.

这将为目标平台配置项目。工具链文件会自动传递,可能还会设置其他特定于平台的变量。

Specifying a custom toolchain file

指定自定义工具链文件

The qt-cmake script passes a Qt-internal toolchain file to CMake. This toolchain file sets several variables that are specific to Qt's target platform.

​qt-cmake脚本将qt内部工具链文件传递给cmake。这个工具链文件设置了几个特定于Qt目标平台的变量。

If you are using a Qt installation that has not been built on your machine, qt-cmake needs to know the location of the CMake toolchain file for the target platform.

如果使用的是尚未在机器上构建的Qt安装,qt-cmake需要知道目标平台的cmake工具链文件的位置。

In such a case, you can instruct qt-cmake to chainload a custom toolchain file by setting the QT_CHAINLOAD_TOOLCHAIN_FILE variable:

在这种情况下,可以通过设置QT_CHAINLOAD_TOOLCHAIN_FILE变量,指示qt-cmake链接加载自定义工具链文件:

~/Qt/6.4.0/android_armv7/bin/qt-cmake -DQT_CHAINLOAD_TOOLCHAIN_FILE=<file-path> -S <source-dir> -B <build-dir>

This instructs Qt's internal toolchain file to load your custom toolchain file as well.

这将指示Qt的内部工具链文件也加载自定义工具链文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值