Making Qt available in CMake projects | Build with CMake | Qt 6.12.0
Description: Provides an overview of how to make Qt's packages available in CMake projects.

- Qt 6.12
- Build with CMake
- Making Qt available in CMake projects
- Qt 6.12.0 Reference Documentation

Supported CMake versions ◦ Imported targets

# Making Qt available in CMake projects

Currently the only supported method of making Qt available for user projects is by having it pre-installed on the system, or any equivalent workflow that builds Qt ahead of time and makes it available to `find_package`, such as `vcpkg`.

When using Qt Creator or `qt-cmake` (`qt-cmake.bat` on Windows), the Qt installation location is automatically forwarded to the underlying CMake call.

When using `cmake` directly, the general search paths used by CMake should cover most non-user installations of Qt. For more control over which Qt package to use, you can pass https://cmake.org/cmake/help/latest/variable/PackageName_ROOT.html `Qt6_ROOT` as an environment or cache variable pointing to the Qt installation path, or by adding or appending to the `CMAKE_PREFIX_PATH` in a similar way. For example, if the installation path is `"~/Qt/6.12.0/gcc_64"`, you would pass it on the command line as `-DQt6_ROOT=$HOME/Qt/6.12.0/gcc_64`.

Note: When cross-compiling (compiling for a platform other than the one you are on, such as WebAssembly or Android) and when using vanilla `cmake`, set `CMAKE_TOOLCHAIN_FILE` instead of search paths like `Qt6_ROOT` or `CMAKE_PREFIX_PATH`. On Linux, the toolchain file (specific to a particular target platform) is typically located at a path similar to this: `"~/Qt/6.12.0/wasm_singlethread/lib/cmake/Qt6/qt.toolchain.cmake"`. It sets the required variables like `CMAKE_PREFIX_PATH`, `CMAKE_FIND_ROOT_PATH`, and `QT_HOST_PATH`.

     cmake_minimum_required(VERSION 3.16)

     project(helloworld)

     find_package(Qt6 REQUIRED)

The Qt package is split into various modules that you need to include depending on the scope of your project, most basic of which is

     find_package(Qt6 REQUIRED COMPONENTS Core)

Modern CMake supports other dependency providers such as `FetchContent`, however using Qt with `FetchContent` is not supported due to a tight integration of Qt features inside CMake itself. Adding Qt sources via `add_subdirectory` and Git submodules is not supported either.

Note: Calling `find_package(Qt6)` in a subdirectory other than where it is used, or inside a `function` is not supported. Doing so can cause various failures in unpredictable locations.

Instead, repeatedly call `find_package(Qt6)` in all subdirectory scopes that use Qt functions, or move the `find_package(Qt6)` call to the root `CMakeLists.txt`.

Supported CMake versions ◦ Imported targets