---
name: "cmake-qt"
section: "7"
platform: "linux"
package: "cmake"
version: "4.4.5-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/cmake-qt"
---

# cmake-qt(7)

CMake Qt Features Reference

## NAME

cmake-qt - CMake Qt Features Reference

## INTRODUCTION

CMake can find and use Qt 4, Qt 5 and Qt 6 libraries. The Qt 4 libraries are found by the **FindQt4** \<#module:FindQt4\> find-module shipped with CMake, whereas the Qt 5 and Qt 6 libraries are found using "Config-file Packages" shipped with Qt 5 and Qt 6. See **[cmake-packages(7)](/cmake-packages/7.md)** \<#manual:[cmake-packages(7)](/cmake-packages/7.md)\> for more information about CMake packages, and see the Qt cmake manual \<https://doc.qt.io/qt-6/cmake-manual.html\> for your Qt version.

Qt 4, Qt 5 and Qt 6 may be used together in the same **CMake buildsystem** \<#manual:[cmake-buildsystem(7)](/cmake-buildsystem/7.md)\>:

```
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
project(Qt4_5_6)
set(CMAKE_AUTOMOC ON)
find_package(Qt6 COMPONENTS Widgets DBus REQUIRED)
add_executable(publisher publisher.cpp)
target_link_libraries(publisher Qt6::Widgets Qt6::DBus)
find_package(Qt5 COMPONENTS Gui DBus REQUIRED)
add_executable(subscriber1 subscriber1.cpp)
target_link_libraries(subscriber1 Qt5::Gui Qt5::DBus)
find_package(Qt4 REQUIRED)
add_executable(subscriber2 subscriber2.cpp)
target_link_libraries(subscriber2 Qt4::QtGui Qt4::QtDBus)
```

A CMake target may not link to more than one Qt version.  A diagnostic is issued if this is attempted or results from transitive target dependency evaluation.

## QT BUILD TOOLS

Qt relies on some bundled tools for code generation, such as **moc** for meta-object code generation, **uic** for widget layout and population, and **rcc** for virtual file system content generation.  These tools may be automatically invoked by **[cmake(1)](/cmake/1.md)** \<#manual:[cmake(1)](/cmake/1.md)\> if the appropriate conditions are met.  The automatic tool invocation may be used with Qt version 4 to 6.

### AUTOMOC

The **AUTOMOC** \<#prop\_tgt:AUTOMOC\> target property controls whether **[cmake(1)](/cmake/1.md)** \<#manual:[cmake(1)](/cmake/1.md)\> inspects the C++ files in the target to determine if they require **moc** to be run, and to create rules to execute **moc** at the appropriate time.

If a macro from **AUTOMOC\_MACRO\_NAMES** \<#prop\_tgt:AUTOMOC\_MACRO\_NAMES\> is found in a header file, **moc** will be run on the file.  The result will be put into a file named according to **moc\_\<basename\>.cpp**. If the macro is found in a C++ implementation file, the moc output will be put into a file named according to **\<basename\>.moc**, following the Qt conventions.  The **\<basename\>.moc** must be included by the user in the C++ implementation file with a preprocessor **#include**.

Included **moc\_\*.cpp** and **\*.moc** files will be generated in the **\<AUTOGEN\_BUILD\_DIR\>/include** directory which is automatically added to the target's **INCLUDE\_DIRECTORIES** \<#prop\_tgt:INCLUDE\_DIRECTORIES\>.

- •

  This differs from CMake 3.7 and below; see their documentation for details.

- •

  For **multi configuration generators** \<#prop\_gbl:GENERATOR\_IS\_MULTI\_CONFIG\>, the include directory is **\<AUTOGEN\_BUILD\_DIR\>/include\_\<CONFIG\>**.

- •

  See **AUTOGEN\_BUILD\_DIR** \<#prop\_tgt:AUTOGEN\_BUILD\_DIR\>.

Not included **moc\_\<basename\>.cpp** files will be generated in custom folders to avoid name collisions and included in a separate file which is compiled into the target, named either **\<AUTOGEN\_BUILD\_DIR\>/mocs\_compilation.cpp** or **\<AUTOGEN\_BUILD\_DIR\>/mocs\_compilation\_$\<CONFIG\>.cpp**.

- •

  See **AUTOGEN\_BUILD\_DIR** \<#prop\_tgt:AUTOGEN\_BUILD\_DIR\>.

The **moc** command line will consume the **COMPILE\_DEFINITIONS** \<#prop\_tgt:COMPILE\_DEFINITIONS\> and **INCLUDE\_DIRECTORIES** \<#prop\_tgt:INCLUDE\_DIRECTORIES\> target properties from the target it is being invoked for, and for the appropriate build configuration.

The **AUTOMOC** \<#prop\_tgt:AUTOMOC\> target property may be pre-set for all following targets by setting the **CMAKE\_AUTOMOC** \<#variable:CMAKE\_AUTOMOC\> variable.  The **AUTOMOC\_MOC\_OPTIONS** \<#prop\_tgt:AUTOMOC\_MOC\_OPTIONS\> target property may be populated to set options to pass to **moc**. The **CMAKE\_AUTOMOC\_MOC\_OPTIONS** \<#variable:CMAKE\_AUTOMOC\_MOC\_OPTIONS\> variable may be populated to pre-set the options for all following targets.

Additional macro names to search for can be added to **AUTOMOC\_MACRO\_NAMES** \<#prop\_tgt:AUTOMOC\_MACRO\_NAMES\>.

Additional **moc** dependency file names can be extracted from source code by using **AUTOMOC\_DEPEND\_FILTERS** \<#prop\_tgt:AUTOMOC\_DEPEND\_FILTERS\>.

Source C++ files can be excluded from **AUTOMOC** \<#prop\_tgt:AUTOMOC\> processing by enabling **SKIP\_AUTOMOC** \<#prop\_sf:SKIP\_AUTOMOC\> or the broader **SKIP\_AUTOGEN** \<#prop\_sf:SKIP\_AUTOGEN\>.

### AUTOUIC

The **AUTOUIC** \<#prop\_tgt:AUTOUIC\> target property controls whether **[cmake(1)](/cmake/1.md)** \<#manual:[cmake(1)](/cmake/1.md)\> inspects the C++ files in the target to determine if they require **uic** to be run, and to create rules to execute **uic** at the appropriate time.

If a preprocessor **#include** directive is found which matches **\<path\>ui\_\<basename\>.h**, and a **\<basename\>.ui** file exists, then **uic** will be executed to generate the appropriate file. The **\<basename\>.ui** file is searched for in the following places

- 1\.

  **\<source\_dir\>/\<basename\>.ui**

- 2\.

  **\<source\_dir\>/\<path\>\<basename\>.ui**

- 3\.

  **\<AUTOUIC\_SEARCH\_PATHS\>/\<basename\>.ui**

- 4\.

  **\<AUTOUIC\_SEARCH\_PATHS\>/\<path\>\<basename\>.ui**

where **\<source\_dir\>** is the directory of the C++ file and **AUTOUIC\_SEARCH\_PATHS** \<#prop\_tgt:AUTOUIC\_SEARCH\_PATHS\> is a list of additional search paths.

The generated generated **ui\_\*.h** files are placed in the **\<AUTOGEN\_BUILD\_DIR\>/include** directory which is automatically added to the target's **INCLUDE\_DIRECTORIES** \<#prop\_tgt:INCLUDE\_DIRECTORIES\>.

- •

  This differs from CMake 3.7 and below; see their documentation for details.

- •

  For **multi configuration generators** \<#prop\_gbl:GENERATOR\_IS\_MULTI\_CONFIG\>, the include directory is **\<AUTOGEN\_BUILD\_DIR\>/include\_\<CONFIG\>**.

- •

  See **AUTOGEN\_BUILD\_DIR** \<#prop\_tgt:AUTOGEN\_BUILD\_DIR\>.

The **AUTOUIC** \<#prop\_tgt:AUTOUIC\> target property may be pre-set for all following targets by setting the **CMAKE\_AUTOUIC** \<#variable:CMAKE\_AUTOUIC\> variable.  The **AUTOUIC\_OPTIONS** \<#prop\_tgt:AUTOUIC\_OPTIONS\> target property may be populated to set options to pass to **uic**.  The **CMAKE\_AUTOUIC\_OPTIONS** \<#variable:CMAKE\_AUTOUIC\_OPTIONS\> variable may be populated to pre-set the options for all following targets.  The **AUTOUIC\_OPTIONS** \<#prop\_sf:AUTOUIC\_OPTIONS\> source file property may be set on the **\<basename\>.ui** file to set particular options for the file.  This overrides options from the **AUTOUIC\_OPTIONS** \<#prop\_tgt:AUTOUIC\_OPTIONS\> target property.

A target may populate the **INTERFACE\_AUTOUIC\_OPTIONS** \<#prop\_tgt:INTERFACE\_AUTOUIC\_OPTIONS\> target property with options that should be used when invoking **uic**.  This must be consistent with the **AUTOUIC\_OPTIONS** \<#prop\_tgt:AUTOUIC\_OPTIONS\> target property content of the depender target.  The **CMAKE\_DEBUG\_TARGET\_PROPERTIES** \<#variable:CMAKE\_DEBUG\_TARGET\_PROPERTIES\> variable may be used to track the origin target of such **INTERFACE\_AUTOUIC\_OPTIONS** \<#prop\_tgt:INTERFACE\_AUTOUIC\_OPTIONS\>.  This means that a library which provides an alternative translation system for Qt may specify options which should be used when running **uic**:

```
add_library(KI18n klocalizedstring.cpp)
target_link_libraries(KI18n Qt6::Core)
# KI18n uses the tr2i18n() function instead of tr().  That function is
# declared in the klocalizedstring.h header.
set(autouic_options
  -tr tr2i18n
  -include klocalizedstring.h
)
set_property(TARGET KI18n APPEND PROPERTY
  INTERFACE_AUTOUIC_OPTIONS ${autouic_options}
)
```

A consuming project linking to the target exported from upstream automatically uses appropriate options when **uic** is run by **AUTOUIC** \<#prop\_tgt:AUTOUIC\>, as a result of linking with the **IMPORTED** \<#prop\_tgt:IMPORTED\> target:

```
set(CMAKE_AUTOUIC ON)
# Uses a libwidget.ui file:
add_library(LibWidget libwidget.cpp)
target_link_libraries(LibWidget
  KF5::KI18n
  Qt5::Widgets
)
```

Source files can be excluded from **AUTOUIC** \<#prop\_tgt:AUTOUIC\> processing by enabling **SKIP\_AUTOUIC** \<#prop\_sf:SKIP\_AUTOUIC\> or the broader **SKIP\_AUTOGEN** \<#prop\_sf:SKIP\_AUTOGEN\>.

### AUTORCC

The **AUTORCC** \<#prop\_tgt:AUTORCC\> target property controls whether **[cmake(1)](/cmake/1.md)** \<#manual:[cmake(1)](/cmake/1.md)\> creates rules to execute **rcc** at the appropriate time on source files which have the suffix **.qrc**.

```
add_executable(myexe main.cpp resource_file.qrc)
```

The **AUTORCC** \<#prop\_tgt:AUTORCC\> target property may be pre-set for all following targets by setting the **CMAKE\_AUTORCC** \<#variable:CMAKE\_AUTORCC\> variable.  The **AUTORCC\_OPTIONS** \<#prop\_tgt:AUTORCC\_OPTIONS\> target property may be populated to set options to pass to **rcc**.  The **CMAKE\_AUTORCC\_OPTIONS** \<#variable:CMAKE\_AUTORCC\_OPTIONS\> variable may be populated to pre-set the options for all following targets.  The **AUTORCC\_OPTIONS** \<#prop\_sf:AUTORCC\_OPTIONS\> source file property may be set on the **\<name\>.qrc** file to set particular options for the file.  This overrides options from the **AUTORCC\_OPTIONS** \<#prop\_tgt:AUTORCC\_OPTIONS\> target property.

Source files can be excluded from **AUTORCC** \<#prop\_tgt:AUTORCC\> processing by enabling **SKIP\_AUTORCC** \<#prop\_sf:SKIP\_AUTORCC\> or the broader **SKIP\_AUTOGEN** \<#prop\_sf:SKIP\_AUTOGEN\>.

## THE \<ORIGIN\>\_AUTOGEN TARGET

The **moc** and **uic** tools are executed as part of a synthesized **\<ORIGIN\>\_autogen** **custom target** \<#command:add\_custom\_target\> generated by CMake.  By default, that **\<ORIGIN\>\_autogen** target inherits the dependencies of the **\<ORIGIN\>** target (see **AUTOGEN\_ORIGIN\_DEPENDS** \<#prop\_tgt:AUTOGEN\_ORIGIN\_DEPENDS\>). Target dependencies may be added to the **\<ORIGIN\>\_autogen** target by adding them to the **AUTOGEN\_TARGET\_DEPENDS** \<#prop\_tgt:AUTOGEN\_TARGET\_DEPENDS\> target property.

**Note:**

If Qt 5.15 or later is used and a depfile is used to track the **moc** dependencies, see The \<ORIGIN\>\_autogen\_timestamp\_deps target.  When using the Visual Studio Generators \<#visual-studio-generators\>, the **\<ORIGIN\>\_autogen** target may not be created at all, see Visual Studio Generators below.

## THE \<ORIGIN\>\_AUTOGEN\_TIMESTAMP\_DEPS TARGET

If Qt 5.15 or later is used and the generator is **Ninja** \<#generator:Ninja\>, a Makefile \<#makefile-generators\> generator, a Visual Studio \<#visual-studio-generators\> generator or **Xcode** \<#generator:Xcode\>, the **moc** dependencies are tracked with a depfile.  In that case the **\<ORIGIN\>\_autogen\_timestamp\_deps** target is also created in addition to the \<ORIGIN\>\_autogen target.  This target does not have any sources or commands to execute, but it has dependencies that were previously inherited by the pre-Qt 5.15 \<ORIGIN\>\_autogen target. These dependencies will serve as a list of order-only dependencies for the custom command, without forcing the custom command to re-execute.

Added in version 4.4: Depfile support for the Visual Studio Generators \<#visual-studio-generators\> and **Xcode** \<#generator:Xcode\>.  Earlier versions used a depfile only for the **Ninja** \<#generator:Ninja\> and Makefile Generators \<#makefile-generators\>.

**Note:**

When using the Visual Studio Generators \<#visual-studio-generators\>, neither this target nor the \<ORIGIN\>\_autogen target is created in the common case, see Visual Studio Generators below.

## VISUAL STUDIO GENERATORS

When using the Visual Studio Generators \<#visual-studio-generators\>, CMake adds the **moc** and **uic** step to the **\<ORIGIN\>** project itself instead of creating the \<ORIGIN\>\_autogen **custom target** \<#command:add\_custom\_target\> (for **AUTOMOC** \<#prop\_tgt:AUTOMOC\> and **AUTOUIC** \<#prop\_tgt:AUTOUIC\>).  With Qt 5.15 or later, the step is a **custom command** \<#command:add\_custom\_command\> with a depfile, and therefore runs only when one of its dependencies changed.  With earlier Qt versions it is a **PRE\_BUILD** command that runs on every build of **\<ORIGIN\>**.

Changed in version 4.4.3: With Qt 5.15 or later, the **moc** and **uic** step is a custom command in the **\<ORIGIN\>** project.  CMake 4.4.0 through 4.4.2 created the \<ORIGIN\>\_autogen and \<ORIGIN\>\_autogen\_timestamp\_deps targets instead.

This isn't always possible though and an \<ORIGIN\>\_autogen **custom target** \<#command:add\_custom\_target\> is used, when either

- •

  the **\<ORIGIN\>** target depends on **GENERATED** \<#prop\_sf:GENERATED\> files which aren't excluded from **AUTOMOC** \<#prop\_tgt:AUTOMOC\> and **AUTOUIC** \<#prop\_tgt:AUTOUIC\> by **SKIP\_AUTOMOC** \<#prop\_sf:SKIP\_AUTOMOC\>, **SKIP\_AUTOUIC** \<#prop\_sf:SKIP\_AUTOUIC\>, **SKIP\_AUTOGEN** \<#prop\_sf:SKIP\_AUTOGEN\> or **CMP0071** \<#policy:CMP0071\>

- •

  **AUTOGEN\_TARGET\_DEPENDS** \<#prop\_tgt:AUTOGEN\_TARGET\_DEPENDS\> lists a source file

- •

  **CMAKE\_GLOBAL\_AUTOGEN\_TARGET** \<#variable:CMAKE\_GLOBAL\_AUTOGEN\_TARGET\> is enabled

With Qt 5.15 or later, the \<ORIGIN\>\_autogen\_timestamp\_deps target is created along with it.

## QTMAIN.LIB ON WINDOWS

The Qt 4 and 5 **IMPORTED** \<#prop\_tgt:IMPORTED\> targets for the QtGui libraries specify that the qtmain.lib static library shipped with Qt will be linked by all dependent executables which have the **WIN32\_EXECUTABLE** \<#prop\_tgt:WIN32\_EXECUTABLE\> enabled.

To disable this behavior, enable the **Qt5\_NO\_LINK\_QTMAIN** target property for Qt 5 based targets or **QT4\_NO\_LINK\_QTMAIN** target property for Qt 4 based targets.

```
add_executable(myexe WIN32 main.cpp)
target_link_libraries(myexe Qt4::QtGui)
add_executable(myexe_no_qtmain WIN32 main_no_qtmain.cpp)
set_property(TARGET main_no_qtmain PROPERTY QT4_NO_LINK_QTMAIN ON)
target_link_libraries(main_no_qtmain Qt4::QtGui)
```

## Copyright

2000-2026 Kitware, Inc. and Contributors
