| .\" Man page generated from reStructuredText. |
| . |
| . |
| .nr rst2man-indent-level 0 |
| . |
| .de1 rstReportMargin |
| \\$1 \\n[an-margin] |
| level \\n[rst2man-indent-level] |
| level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] |
| - |
| \\n[rst2man-indent0] |
| \\n[rst2man-indent1] |
| \\n[rst2man-indent2] |
| .. |
| .de1 INDENT |
| .\" .rstReportMargin pre: |
| . RS \\$1 |
| . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] |
| . nr rst2man-indent-level +1 |
| .\" .rstReportMargin post: |
| .. |
| .de UNINDENT |
| . RE |
| .\" indent \\n[an-margin] |
| .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] |
| .nr rst2man-indent-level -1 |
| .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] |
| .in \\n[rst2man-indent\\n[rst2man-indent-level]]u |
| .. |
| .TH "CMAKE-QT" "7" "Jan 23, 2025" "3.31.5" "CMake" |
| .SH NAME |
| cmake-qt \- CMake Qt Features Reference |
| .SH INTRODUCTION |
| .sp |
| CMake can find and use Qt 4, Qt 5 and Qt 6 libraries. The Qt 4 libraries are |
| found by the \X'tty: link #module:FindQt4'\fI\%FindQt4\fP\X'tty: link' find\-module shipped with CMake, whereas the |
| Qt 5 and Qt 6 libraries are found using \(dqConfig\-file Packages\(dq shipped with |
| Qt 5 and Qt 6. See \X'tty: link #manual:cmake-packages(7)'\fI\%cmake\-packages(7)\fP\X'tty: link' for more information about CMake |
| packages, and see \X'tty: link https://doc.qt.io/qt-6/cmake-manual.html'\fI\%the Qt cmake manual\fP\X'tty: link' for your Qt version. |
| .sp |
| Qt 4, Qt 5 and Qt 6 may be used together in the same |
| \X'tty: link #manual:cmake-buildsystem(7)'\fI\%CMake buildsystem\fP\X'tty: link': |
| .INDENT 0.0 |
| .INDENT 3.5 |
| .sp |
| .nf |
| .ft C |
| 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) |
| .ft P |
| .fi |
| .UNINDENT |
| .UNINDENT |
| .sp |
| 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. |
| .SH QT BUILD TOOLS |
| .sp |
| Qt relies on some bundled tools for code generation, such as \fBmoc\fP for |
| meta\-object code generation, \fBuic\fP for widget layout and population, |
| and \fBrcc\fP for virtual file system content generation. These tools may be |
| automatically invoked by \X'tty: link #manual:cmake(1)'\fI\%cmake(1)\fP\X'tty: link' if the appropriate conditions |
| are met. The automatic tool invocation may be used with Qt version 4 to 6. |
| .SS AUTOMOC |
| .sp |
| The \X'tty: link #prop_tgt:AUTOMOC'\fI\%AUTOMOC\fP\X'tty: link' target property controls whether \X'tty: link #manual:cmake(1)'\fI\%cmake(1)\fP\X'tty: link' |
| inspects the C++ files in the target to determine if they require \fBmoc\fP to |
| be run, and to create rules to execute \fBmoc\fP at the appropriate time. |
| .sp |
| If a macro from \X'tty: link #prop_tgt:AUTOMOC_MACRO_NAMES'\fI\%AUTOMOC_MACRO_NAMES\fP\X'tty: link' is found in a header file, |
| \fBmoc\fP will be run on the file. The result will be put into a file named |
| according to \fBmoc_<basename>.cpp\fP\&. |
| If the macro is found in a C++ implementation |
| file, the moc output will be put into a file named according to |
| \fB<basename>.moc\fP, following the Qt conventions. The \fB<basename>.moc\fP must |
| be included by the user in the C++ implementation file with a preprocessor |
| \fB#include\fP\&. |
| .sp |
| Included \fBmoc_*.cpp\fP and \fB*.moc\fP files will be generated in the |
| \fB<AUTOGEN_BUILD_DIR>/include\fP directory which is |
| automatically added to the target\(aqs \X'tty: link #prop_tgt:INCLUDE_DIRECTORIES'\fI\%INCLUDE_DIRECTORIES\fP\X'tty: link'\&. |
| .INDENT 0.0 |
| .IP \(bu 2 |
| This differs from CMake 3.7 and below; see their documentation for details. |
| .IP \(bu 2 |
| For \X'tty: link #prop_gbl:GENERATOR_IS_MULTI_CONFIG'\fI\%multi configuration generators\fP\X'tty: link', |
| the include directory is \fB<AUTOGEN_BUILD_DIR>/include_<CONFIG>\fP\&. |
| .IP \(bu 2 |
| See \X'tty: link #prop_tgt:AUTOGEN_BUILD_DIR'\fI\%AUTOGEN_BUILD_DIR\fP\X'tty: link'\&. |
| .UNINDENT |
| .sp |
| Not included \fBmoc_<basename>.cpp\fP 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 |
| \fB<AUTOGEN_BUILD_DIR>/mocs_compilation.cpp\fP or |
| \fB<AUTOGEN_BUILD_DIR>/mocs_compilation_$<CONFIG>.cpp\fP\&. |
| .INDENT 0.0 |
| .IP \(bu 2 |
| See \X'tty: link #prop_tgt:AUTOGEN_BUILD_DIR'\fI\%AUTOGEN_BUILD_DIR\fP\X'tty: link'\&. |
| .UNINDENT |
| .sp |
| The \fBmoc\fP command line will consume the \X'tty: link #prop_tgt:COMPILE_DEFINITIONS'\fI\%COMPILE_DEFINITIONS\fP\X'tty: link' and |
| \X'tty: link #prop_tgt:INCLUDE_DIRECTORIES'\fI\%INCLUDE_DIRECTORIES\fP\X'tty: link' target properties from the target it is being |
| invoked for, and for the appropriate build configuration. |
| .sp |
| The \X'tty: link #prop_tgt:AUTOMOC'\fI\%AUTOMOC\fP\X'tty: link' target property may be pre\-set for all |
| following targets by setting the \X'tty: link #variable:CMAKE_AUTOMOC'\fI\%CMAKE_AUTOMOC\fP\X'tty: link' variable. The |
| \X'tty: link #prop_tgt:AUTOMOC_MOC_OPTIONS'\fI\%AUTOMOC_MOC_OPTIONS\fP\X'tty: link' target property may be populated to set |
| options to pass to \fBmoc\fP\&. The \X'tty: link #variable:CMAKE_AUTOMOC_MOC_OPTIONS'\fI\%CMAKE_AUTOMOC_MOC_OPTIONS\fP\X'tty: link' |
| variable may be populated to pre\-set the options for all following targets. |
| .sp |
| Additional macro names to search for can be added to |
| \X'tty: link #prop_tgt:AUTOMOC_MACRO_NAMES'\fI\%AUTOMOC_MACRO_NAMES\fP\X'tty: link'\&. |
| .sp |
| Additional \fBmoc\fP dependency file names can be extracted from source code |
| by using \X'tty: link #prop_tgt:AUTOMOC_DEPEND_FILTERS'\fI\%AUTOMOC_DEPEND_FILTERS\fP\X'tty: link'\&. |
| .sp |
| Source C++ files can be excluded from \X'tty: link #prop_tgt:AUTOMOC'\fI\%AUTOMOC\fP\X'tty: link' processing by |
| enabling \X'tty: link #prop_sf:SKIP_AUTOMOC'\fI\%SKIP_AUTOMOC\fP\X'tty: link' or the broader \X'tty: link #prop_sf:SKIP_AUTOGEN'\fI\%SKIP_AUTOGEN\fP\X'tty: link'\&. |
| .SS AUTOUIC |
| .sp |
| The \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link' target property controls whether \X'tty: link #manual:cmake(1)'\fI\%cmake(1)\fP\X'tty: link' |
| inspects the C++ files in the target to determine if they require \fBuic\fP to |
| be run, and to create rules to execute \fBuic\fP at the appropriate time. |
| .sp |
| If a preprocessor \fB#include\fP directive is found which matches |
| \fB<path>ui_<basename>.h\fP, and a \fB<basename>.ui\fP file exists, |
| then \fBuic\fP will be executed to generate the appropriate file. |
| The \fB<basename>.ui\fP file is searched for in the following places |
| .INDENT 0.0 |
| .IP 1. 3 |
| \fB<source_dir>/<basename>.ui\fP |
| .IP 2. 3 |
| \fB<source_dir>/<path><basename>.ui\fP |
| .IP 3. 3 |
| \fB<AUTOUIC_SEARCH_PATHS>/<basename>.ui\fP |
| .IP 4. 3 |
| \fB<AUTOUIC_SEARCH_PATHS>/<path><basename>.ui\fP |
| .UNINDENT |
| .sp |
| where \fB<source_dir>\fP is the directory of the C++ file and |
| \X'tty: link #prop_tgt:AUTOUIC_SEARCH_PATHS'\fI\%AUTOUIC_SEARCH_PATHS\fP\X'tty: link' is a list of additional search paths. |
| .sp |
| The generated generated \fBui_*.h\fP files are placed in the |
| \fB<AUTOGEN_BUILD_DIR>/include\fP directory which is |
| automatically added to the target\(aqs \X'tty: link #prop_tgt:INCLUDE_DIRECTORIES'\fI\%INCLUDE_DIRECTORIES\fP\X'tty: link'\&. |
| .INDENT 0.0 |
| .IP \(bu 2 |
| This differs from CMake 3.7 and below; see their documentation for details. |
| .IP \(bu 2 |
| For \X'tty: link #prop_gbl:GENERATOR_IS_MULTI_CONFIG'\fI\%multi configuration generators\fP\X'tty: link', |
| the include directory is \fB<AUTOGEN_BUILD_DIR>/include_<CONFIG>\fP\&. |
| .IP \(bu 2 |
| See \X'tty: link #prop_tgt:AUTOGEN_BUILD_DIR'\fI\%AUTOGEN_BUILD_DIR\fP\X'tty: link'\&. |
| .UNINDENT |
| .sp |
| The \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link' target property may be pre\-set for all following |
| targets by setting the \X'tty: link #variable:CMAKE_AUTOUIC'\fI\%CMAKE_AUTOUIC\fP\X'tty: link' variable. The |
| \X'tty: link #prop_tgt:AUTOUIC_OPTIONS'\fI\%AUTOUIC_OPTIONS\fP\X'tty: link' target property may be populated to set options |
| to pass to \fBuic\fP\&. The \X'tty: link #variable:CMAKE_AUTOUIC_OPTIONS'\fI\%CMAKE_AUTOUIC_OPTIONS\fP\X'tty: link' variable may be |
| populated to pre\-set the options for all following targets. The |
| \X'tty: link #prop_sf:AUTOUIC_OPTIONS'\fI\%AUTOUIC_OPTIONS\fP\X'tty: link' source file property may be set on the |
| \fB<basename>.ui\fP file to set particular options for the file. This |
| overrides options from the \X'tty: link #prop_tgt:AUTOUIC_OPTIONS'\fI\%AUTOUIC_OPTIONS\fP\X'tty: link' target property. |
| .sp |
| A target may populate the \X'tty: link #prop_tgt:INTERFACE_AUTOUIC_OPTIONS'\fI\%INTERFACE_AUTOUIC_OPTIONS\fP\X'tty: link' target |
| property with options that should be used when invoking \fBuic\fP\&. This must be |
| consistent with the \X'tty: link #prop_tgt:AUTOUIC_OPTIONS'\fI\%AUTOUIC_OPTIONS\fP\X'tty: link' target property content of the |
| depender target. The \X'tty: link #variable:CMAKE_DEBUG_TARGET_PROPERTIES'\fI\%CMAKE_DEBUG_TARGET_PROPERTIES\fP\X'tty: link' variable may |
| be used to track the origin target of such |
| \X'tty: link #prop_tgt:INTERFACE_AUTOUIC_OPTIONS'\fI\%INTERFACE_AUTOUIC_OPTIONS\fP\X'tty: link'\&. This means that a library which |
| provides an alternative translation system for Qt may specify options which |
| should be used when running \fBuic\fP: |
| .INDENT 0.0 |
| .INDENT 3.5 |
| .sp |
| .nf |
| .ft C |
| 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} |
| ) |
| .ft P |
| .fi |
| .UNINDENT |
| .UNINDENT |
| .sp |
| A consuming project linking to the target exported from upstream automatically |
| uses appropriate options when \fBuic\fP is run by \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link', as a |
| result of linking with the \X'tty: link #prop_tgt:IMPORTED'\fI\%IMPORTED\fP\X'tty: link' target: |
| .INDENT 0.0 |
| .INDENT 3.5 |
| .sp |
| .nf |
| .ft C |
| set(CMAKE_AUTOUIC ON) |
| # Uses a libwidget.ui file: |
| add_library(LibWidget libwidget.cpp) |
| target_link_libraries(LibWidget |
| KF5::KI18n |
| Qt5::Widgets |
| ) |
| .ft P |
| .fi |
| .UNINDENT |
| .UNINDENT |
| .sp |
| Source files can be excluded from \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link' processing by |
| enabling \X'tty: link #prop_sf:SKIP_AUTOUIC'\fI\%SKIP_AUTOUIC\fP\X'tty: link' or the broader \X'tty: link #prop_sf:SKIP_AUTOGEN'\fI\%SKIP_AUTOGEN\fP\X'tty: link'\&. |
| .SS AUTORCC |
| .sp |
| The \X'tty: link #prop_tgt:AUTORCC'\fI\%AUTORCC\fP\X'tty: link' target property controls whether \X'tty: link #manual:cmake(1)'\fI\%cmake(1)\fP\X'tty: link' |
| creates rules to execute \fBrcc\fP at the appropriate time on source files |
| which have the suffix \fB\&.qrc\fP\&. |
| .INDENT 0.0 |
| .INDENT 3.5 |
| .sp |
| .nf |
| .ft C |
| add_executable(myexe main.cpp resource_file.qrc) |
| .ft P |
| .fi |
| .UNINDENT |
| .UNINDENT |
| .sp |
| The \X'tty: link #prop_tgt:AUTORCC'\fI\%AUTORCC\fP\X'tty: link' target property may be pre\-set for all following targets |
| by setting the \X'tty: link #variable:CMAKE_AUTORCC'\fI\%CMAKE_AUTORCC\fP\X'tty: link' variable. The |
| \X'tty: link #prop_tgt:AUTORCC_OPTIONS'\fI\%AUTORCC_OPTIONS\fP\X'tty: link' target property may be populated to set options |
| to pass to \fBrcc\fP\&. The \X'tty: link #variable:CMAKE_AUTORCC_OPTIONS'\fI\%CMAKE_AUTORCC_OPTIONS\fP\X'tty: link' variable may be |
| populated to pre\-set the options for all following targets. The |
| \X'tty: link #prop_sf:AUTORCC_OPTIONS'\fI\%AUTORCC_OPTIONS\fP\X'tty: link' source file property may be set on the |
| \fB<name>.qrc\fP file to set particular options for the file. This |
| overrides options from the \X'tty: link #prop_tgt:AUTORCC_OPTIONS'\fI\%AUTORCC_OPTIONS\fP\X'tty: link' target property. |
| .sp |
| Source files can be excluded from \X'tty: link #prop_tgt:AUTORCC'\fI\%AUTORCC\fP\X'tty: link' processing by |
| enabling \X'tty: link #prop_sf:SKIP_AUTORCC'\fI\%SKIP_AUTORCC\fP\X'tty: link' or the broader \X'tty: link #prop_sf:SKIP_AUTOGEN'\fI\%SKIP_AUTOGEN\fP\X'tty: link'\&. |
| .SH THE <ORIGIN>_AUTOGEN TARGET |
| .sp |
| The \fBmoc\fP and \fBuic\fP tools are executed as part of a synthesized |
| \fI\%The <ORIGIN>_autogen target\fP \X'tty: link #command:add_custom_target'\fI\%custom target\fP\X'tty: link' generated by |
| CMake. By default that \fI\%The <ORIGIN>_autogen target\fP target inherits the dependencies |
| of the \fB<ORIGIN>\fP target (see \X'tty: link #prop_tgt:AUTOGEN_ORIGIN_DEPENDS'\fI\%AUTOGEN_ORIGIN_DEPENDS\fP\X'tty: link'). |
| Target dependencies may be added to the \fI\%The <ORIGIN>_autogen target\fP target by adding |
| them to the \X'tty: link #prop_tgt:AUTOGEN_TARGET_DEPENDS'\fI\%AUTOGEN_TARGET_DEPENDS\fP\X'tty: link' target property. |
| .sp |
| \fBNOTE:\fP |
| .INDENT 0.0 |
| .INDENT 3.5 |
| If Qt 5.15 or later is used and the generator is either \X'tty: link #generator:Ninja'\fI\%Ninja\fP\X'tty: link' or |
| \X'tty: link #makefile-generators'\fI\%Makefile Generators\fP\X'tty: link', see \fI\%The <ORIGIN>_autogen_timestamp_deps target\fP\&. |
| .UNINDENT |
| .UNINDENT |
| .SH THE <ORIGIN>_AUTOGEN_TIMESTAMP_DEPS TARGET |
| .sp |
| If Qt 5.15 or later is used and the generator is either \X'tty: link #generator:Ninja'\fI\%Ninja\fP\X'tty: link' or |
| \X'tty: link #makefile-generators'\fI\%Makefile Generators\fP\X'tty: link', the \fB<ORIGIN>_autogen_timestamp_deps\fP target is |
| also created in addition to the \fI\%The <ORIGIN>_autogen target\fP 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 \fI\%The <ORIGIN>_autogen target\fP target. |
| These dependencies will serve as a list of order\-only dependencies for the |
| custom command, without forcing the custom command to re\-execute. |
| .SH VISUAL STUDIO GENERATORS |
| .sp |
| When using the \X'tty: link #manual:cmake-generators(7)'\fI\%Visual Studio generators\fP\X'tty: link', CMake |
| generates a \fBPRE_BUILD\fP \X'tty: link #command:add_custom_command'\fI\%custom command\fP\X'tty: link' |
| instead of the \fI\%The <ORIGIN>_autogen target\fP |
| \X'tty: link #command:add_custom_target'\fI\%custom target\fP\X'tty: link' (for \X'tty: link #prop_tgt:AUTOMOC'\fI\%AUTOMOC\fP\X'tty: link' and |
| \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link'). This isn\(aqt always possible though and an |
| \fI\%The <ORIGIN>_autogen target\fP \X'tty: link #command:add_custom_target'\fI\%custom target\fP\X'tty: link' is used, |
| when either |
| .INDENT 0.0 |
| .IP \(bu 2 |
| the \fB<ORIGIN>\fP target depends on \X'tty: link #prop_sf:GENERATED'\fI\%GENERATED\fP\X'tty: link' files which aren\(aqt |
| excluded from \X'tty: link #prop_tgt:AUTOMOC'\fI\%AUTOMOC\fP\X'tty: link' and \X'tty: link #prop_tgt:AUTOUIC'\fI\%AUTOUIC\fP\X'tty: link' by |
| \X'tty: link #prop_sf:SKIP_AUTOMOC'\fI\%SKIP_AUTOMOC\fP\X'tty: link', \X'tty: link #prop_sf:SKIP_AUTOUIC'\fI\%SKIP_AUTOUIC\fP\X'tty: link', \X'tty: link #prop_sf:SKIP_AUTOGEN'\fI\%SKIP_AUTOGEN\fP\X'tty: link' |
| or \X'tty: link #policy:CMP0071'\fI\%CMP0071\fP\X'tty: link' |
| .IP \(bu 2 |
| \X'tty: link #prop_tgt:AUTOGEN_TARGET_DEPENDS'\fI\%AUTOGEN_TARGET_DEPENDS\fP\X'tty: link' lists a source file |
| .IP \(bu 2 |
| \X'tty: link #variable:CMAKE_GLOBAL_AUTOGEN_TARGET'\fI\%CMAKE_GLOBAL_AUTOGEN_TARGET\fP\X'tty: link' is enabled |
| .UNINDENT |
| .SH QTMAIN.LIB ON WINDOWS |
| .sp |
| The Qt 4 and 5 \X'tty: link #prop_tgt:IMPORTED'\fI\%IMPORTED\fP\X'tty: link' 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 \X'tty: link #prop_tgt:WIN32_EXECUTABLE'\fI\%WIN32_EXECUTABLE\fP\X'tty: link' enabled. |
| .sp |
| To disable this behavior, enable the \fBQt5_NO_LINK_QTMAIN\fP target property for |
| Qt 5 based targets or \fBQT4_NO_LINK_QTMAIN\fP target property for Qt 4 based |
| targets. |
| .INDENT 0.0 |
| .INDENT 3.5 |
| .sp |
| .nf |
| .ft C |
| 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) |
| .ft P |
| .fi |
| .UNINDENT |
| .UNINDENT |
| .SH COPYRIGHT |
| 2000-2024 Kitware, Inc. and Contributors |
| .\" Generated by docutils manpage writer. |
| . |
| |