.\" 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" "April 14, 2024" "3.29.2" "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 \fI\%FindQt4\fP 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 \fI\%cmake\-packages(7)\fP for more information about CMake packages, and see \fI\%the Qt cmake manual\fP for your Qt version. .sp Qt 4, Qt 5 and Qt 6 may be used together in the same \fI\%CMake buildsystem\fP: .INDENT 0.0 .INDENT 3.5 .sp .EX 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) .EE .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 \fI\%cmake(1)\fP if the appropriate conditions are met. The automatic tool invocation may be used with Qt version 4 to 6. .SS AUTOMOC .sp The \fI\%AUTOMOC\fP target property controls whether \fI\%cmake(1)\fP 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 \fI\%AUTOMOC_MACRO_NAMES\fP 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_.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.moc\fP, following the Qt conventions. The \fB.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/include\fP directory which is automatically added to the target\(aqs \fI\%INCLUDE_DIRECTORIES\fP\&. .INDENT 0.0 .IP \(bu 2 This differs from CMake 3.7 and below; see their documentation for details. .IP \(bu 2 For \fI\%multi configuration generators\fP, the include directory is \fB/include_\fP\&. .IP \(bu 2 See \fI\%AUTOGEN_BUILD_DIR\fP\&. .UNINDENT .sp Not included \fBmoc_.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/mocs_compilation.cpp\fP or \fB/mocs_compilation_$.cpp\fP\&. .INDENT 0.0 .IP \(bu 2 See \fI\%AUTOGEN_BUILD_DIR\fP\&. .UNINDENT .sp The \fBmoc\fP command line will consume the \fI\%COMPILE_DEFINITIONS\fP and \fI\%INCLUDE_DIRECTORIES\fP target properties from the target it is being invoked for, and for the appropriate build configuration. .sp The \fI\%AUTOMOC\fP target property may be pre\-set for all following targets by setting the \fI\%CMAKE_AUTOMOC\fP variable. The \fI\%AUTOMOC_MOC_OPTIONS\fP target property may be populated to set options to pass to \fBmoc\fP\&. The \fI\%CMAKE_AUTOMOC_MOC_OPTIONS\fP variable may be populated to pre\-set the options for all following targets. .sp Additional macro names to search for can be added to \fI\%AUTOMOC_MACRO_NAMES\fP\&. .sp Additional \fBmoc\fP dependency file names can be extracted from source code by using \fI\%AUTOMOC_DEPEND_FILTERS\fP\&. .sp Source C++ files can be excluded from \fI\%AUTOMOC\fP processing by enabling \fI\%SKIP_AUTOMOC\fP or the broader \fI\%SKIP_AUTOGEN\fP\&. .SS AUTOUIC .sp The \fI\%AUTOUIC\fP target property controls whether \fI\%cmake(1)\fP 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 \fBui_.h\fP, and a \fB.ui\fP file exists, then \fBuic\fP will be executed to generate the appropriate file. The \fB.ui\fP file is searched for in the following places .INDENT 0.0 .IP 1. 3 \fB/.ui\fP .IP 2. 3 \fB/.ui\fP .IP 3. 3 \fB/.ui\fP .IP 4. 3 \fB/.ui\fP .UNINDENT .sp where \fB\fP is the directory of the C++ file and \fI\%AUTOUIC_SEARCH_PATHS\fP is a list of additional search paths. .sp The generated generated \fBui_*.h\fP files are placed in the \fB/include\fP directory which is automatically added to the target\(aqs \fI\%INCLUDE_DIRECTORIES\fP\&. .INDENT 0.0 .IP \(bu 2 This differs from CMake 3.7 and below; see their documentation for details. .IP \(bu 2 For \fI\%multi configuration generators\fP, the include directory is \fB/include_\fP\&. .IP \(bu 2 See \fI\%AUTOGEN_BUILD_DIR\fP\&. .UNINDENT .sp The \fI\%AUTOUIC\fP target property may be pre\-set for all following targets by setting the \fI\%CMAKE_AUTOUIC\fP variable. The \fI\%AUTOUIC_OPTIONS\fP target property may be populated to set options to pass to \fBuic\fP\&. The \fI\%CMAKE_AUTOUIC_OPTIONS\fP variable may be populated to pre\-set the options for all following targets. The \fI\%AUTOUIC_OPTIONS\fP source file property may be set on the \fB.ui\fP file to set particular options for the file. This overrides options from the \fI\%AUTOUIC_OPTIONS\fP target property. .sp A target may populate the \fI\%INTERFACE_AUTOUIC_OPTIONS\fP target property with options that should be used when invoking \fBuic\fP\&. This must be consistent with the \fI\%AUTOUIC_OPTIONS\fP target property content of the depender target. The \fI\%CMAKE_DEBUG_TARGET_PROPERTIES\fP variable may be used to track the origin target of such \fI\%INTERFACE_AUTOUIC_OPTIONS\fP\&. 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 .EX 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} ) .EE .UNINDENT .UNINDENT .sp A consuming project linking to the target exported from upstream automatically uses appropriate options when \fBuic\fP is run by \fI\%AUTOUIC\fP, as a result of linking with the \fI\%IMPORTED\fP target: .INDENT 0.0 .INDENT 3.5 .sp .EX set(CMAKE_AUTOUIC ON) # Uses a libwidget.ui file: add_library(LibWidget libwidget.cpp) target_link_libraries(LibWidget KF5::KI18n Qt5::Widgets ) .EE .UNINDENT .UNINDENT .sp Source files can be excluded from \fI\%AUTOUIC\fP processing by enabling \fI\%SKIP_AUTOUIC\fP or the broader \fI\%SKIP_AUTOGEN\fP\&. .SS AUTORCC .sp The \fI\%AUTORCC\fP target property controls whether \fI\%cmake(1)\fP 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 .EX add_executable(myexe main.cpp resource_file.qrc) .EE .UNINDENT .UNINDENT .sp The \fI\%AUTORCC\fP target property may be pre\-set for all following targets by setting the \fI\%CMAKE_AUTORCC\fP variable. The \fI\%AUTORCC_OPTIONS\fP target property may be populated to set options to pass to \fBrcc\fP\&. The \fI\%CMAKE_AUTORCC_OPTIONS\fP variable may be populated to pre\-set the options for all following targets. The \fI\%AUTORCC_OPTIONS\fP source file property may be set on the \fB.qrc\fP file to set particular options for the file. This overrides options from the \fI\%AUTORCC_OPTIONS\fP target property. .sp Source files can be excluded from \fI\%AUTORCC\fP processing by enabling \fI\%SKIP_AUTORCC\fP or the broader \fI\%SKIP_AUTOGEN\fP\&. .SH THE _AUTOGEN TARGET .sp The \fBmoc\fP and \fBuic\fP tools are executed as part of a synthesized \fI\%The _autogen target\fP \fI\%custom target\fP generated by CMake. By default that \fI\%The _autogen target\fP target inherits the dependencies of the \fB\fP target (see \fI\%AUTOGEN_ORIGIN_DEPENDS\fP). Target dependencies may be added to the \fI\%The _autogen target\fP target by adding them to the \fI\%AUTOGEN_TARGET_DEPENDS\fP target property. .sp \fBNOTE:\fP .INDENT 0.0 .INDENT 3.5 If Qt 5.15 or later is used and the generator is either \fI\%Ninja\fP or \fI\%Makefile Generators\fP, see \fI\%The _autogen_timestamp_deps target\fP\&. .UNINDENT .UNINDENT .SH THE _AUTOGEN_TIMESTAMP_DEPS TARGET .sp If Qt 5.15 or later is used and the generator is either \fI\%Ninja\fP or \fI\%Makefile Generators\fP, the \fB_autogen_timestamp_deps\fP target is also created in addition to the \fI\%The _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 _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 \fI\%Visual Studio generators\fP, CMake generates a \fBPRE_BUILD\fP \fI\%custom command\fP instead of the \fI\%The _autogen target\fP \fI\%custom target\fP (for \fI\%AUTOMOC\fP and \fI\%AUTOUIC\fP). This isn\(aqt always possible though and an \fI\%The _autogen target\fP \fI\%custom target\fP is used, when either .INDENT 0.0 .IP \(bu 2 the \fB\fP target depends on \fI\%GENERATED\fP files which aren\(aqt excluded from \fI\%AUTOMOC\fP and \fI\%AUTOUIC\fP by \fI\%SKIP_AUTOMOC\fP, \fI\%SKIP_AUTOUIC\fP, \fI\%SKIP_AUTOGEN\fP or \fI\%CMP0071\fP .IP \(bu 2 \fI\%AUTOGEN_TARGET_DEPENDS\fP lists a source file .IP \(bu 2 \fI\%CMAKE_GLOBAL_AUTOGEN_TARGET\fP is enabled .UNINDENT .SH QTMAIN.LIB ON WINDOWS .sp The Qt 4 and 5 \fI\%IMPORTED\fP 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 \fI\%WIN32_EXECUTABLE\fP 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 .EX 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) .EE .UNINDENT .UNINDENT .SH COPYRIGHT 2000-2024 Kitware, Inc. and Contributors .\" Generated by docutils manpage writer. .