部署

Use Qt's CMake deployment API to deploy Qt projects for desktop platforms. The API supports various target platforms, which you can find in the qt_generate_deploy_app_script() 文档编制。

The deployment process depends on whether your project is a Qt Widgets application or a Qt Quick application. Even if you are planning to deploy only Qt Quick applications, read about deploying Qt Widgets applications first to understand the process.

Deploying a Qt Widgets application

This section shows how to deploy a Qt Widgets application with an example of a simple C++ Qt project.

cmake_minimum_required(VERSION 3.16)
project(MyApp VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Core)
qt_standard_project_setup()
qt_add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE Qt6::Core)
					

You need to instruct CMake to install the application target into the appropriate location. On macOS, bundles are installed directly into ${CMAKE_INSTALL_PREFIX} , on other platforms into the "bin" directory underneath.

install(TARGETS MyApp
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
					

注意, qt_standard_project_setup() pulls in CMake's GNUInstallDirs.cmake . This is what defines the CMAKE_INSTALL_BINDIR 变量。

You also need to generate a deployment script . A deployment script is CMake code that is executed at installation time. This code takes care of collecting runtime dependencies and copies them to the installation directory.

qt_generate_deploy_app_script(
    TARGET MyApp
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
					

The qt_generate_deploy_app_script() command generates the deployment script in the build directory. The file name of the generated script file is stored in the deploy_script variable. The install(SCRIPT) call instructs CMake to run the script on installation.

The project can be installed with cmake --install . or ninja install like any other CMake-based project. After installation, the installation directory contains the shared libraries and assets that are necessary to run the application. In other words, the installation produces a self-contained directory, ready to be packaged - for example by cpack .

Deploying a Qt Quick application

Deploying a Qt Quick project requires a different command to generate the deployment script. The rest of the steps are similar to deploying a Qt Widgets application.

First, you create the Qt Quick application.

cmake_minimum_required(VERSION 3.16)
project(MyApp VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Core)
qt_standard_project_setup()
qt_add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE Qt6::Core)
					

You install the application as before.

install(TARGETS MyApp
    BUNDLE  DESTINATION .
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
					

To generate the deployment script, you call qt_generate_deploy_qml_app_script() 而不是 qt_generate_deploy_app_script() .

qt_generate_deploy_app_script(
    TARGET MyApp
    OUTPUT_SCRIPT deploy_script
    NO_UNSUPPORTED_PLATFORM_ERROR
)
install(SCRIPT ${deploy_script})
					

On installation, the application binary will be deployed, including the QML files and the shared libraries and assets of Qt that are used by the project. Again, the resulting directory is self-contained and can be packaged by tools like cpack .