Autouic
Should the target be processed with auto-moc (for Qt projects).
AUTOUIC: a boolean specifying whether CMake will handle the Qt uic code generator automatically, i.e. Without having to use the QT5WRAPUI macro. AUTORCC: a boolean specifying whether CMake will handle the Qt rcc code generator automatically, i.e. Without having to use the QT5ADDRESOURCES macro. After numerous promises of how Qbs will be the Qt’s default build system, The Qt Company suddenly killed it, announced that qmake is a dead man walking too, and actually Qt is switching to CMake. So I guess we should start using CMake for building Qt applications as well. Let’s see then what it takes to switch from qmake to CMake with a couple of basic examples. I also wanted to try Qt.
AUTOMOC
is a boolean specifying whether CMake will handle the Qtmoc
preprocessor automatically, i.e. without having to use theQT4_WRAP_CPP()
or QT5_WRAP_CPP()
macro.Currently Qt4 and Qt5 are supported.
This property is initialized by the value of the CMAKE_AUTOMOC
variable if it is set when a target is created.
When this property is set ON
, CMake will scan the header andsource files at build time and invoke moc
accordingly.
Autouch
Header file processing¶
At configuration time, a list of header files that should be scanned byAUTOMOC
is computed from the target's sources.
All header files in the target's sources are added to the scan list.
For all C++ source files
<source_base>.<source_extension>
in thetarget's sources, CMake searches fora regular header with the same base name(
<source_base>.<header_extention>
) anda private header with the same base name and a
_p
suffix(<source_base>_p.<header_extention>
)
and adds these to the scan list.
At build time, CMake scans each unknown or modified header file from thelist and searches for
a Qt macro from
AUTOMOC_MACRO_NAMES
,additional file dependencies from the
FILE
argument of aQ_PLUGIN_METADATA
macro andadditional file dependencies detected by filters defined in
AUTOMOC_DEPEND_FILTERS
.


Autouic Subprocess Error
If a Qt macro is found, then the header will be compiled by the moc
to theoutput file moc_<base_name>.cpp
. The complete output file path isdescribed in the section Output file location.
The header will be moc
compiled again if a file from the additional filedependencies changes.
Header moc
output files moc_<base_name>.cpp
can be included in sourcefiles. In the section Including header moc files in sources there is moreinformation on that topic.
Source file processing¶
At build time, CMake scans each unknown or modified C++ source file from thetarget's sources for
a Qt macro from
AUTOMOC_MACRO_NAMES
,includes of header
moc
files(see Including header moc files in sources),additional file dependencies from the
FILE
argument of aQ_PLUGIN_METADATA
macro andadditional file dependencies detected by filters defined in
AUTOMOC_DEPEND_FILTERS
.
Autouic Example

If a Qt macro is found, then the C++ source file<base>.<source_extension>
is expected to as well contain an includestatement
The source file then will be compiled by the moc
to the output file<base>.moc
. A description of the complete output file path is in sectionOutput file location.
The source will be moc
compiled again if a file from the additional filedependencies changes.
Including header moc files in sources¶
A source file can include the moc
output file of a header<header_base>.<header_extension>
by using an include statement ofthe form
If the moc
output file of a header is included by a source, it willbe generated in a different location than if it was not included. This isdescribed in the section Output file location.
Output file location¶
Included moc output files¶
moc
output files that are included by a source file will be generated in
<AUTOGEN_BUILD_DIR>/include
for single configuration generators or in<AUTOGEN_BUILD_DIR>/include_<CONFIG>
formulticonfiguration
generators.
Where <AUTOGEN_BUILD_DIR>
is the value of the target propertyAUTOGEN_BUILD_DIR
.
The include directory is automatically added to the target'sINCLUDE_DIRECTORIES
.
Not included moc output files¶
moc
output files that are not included in a source file will be generatedin
<AUTOGEN_BUILD_DIR>/<SOURCE_DIR_CHECKSUM>
for single configuration generators or in,<AUTOGEN_BUILD_DIR>/include_<CONFIG>/<SOURCE_DIR_CHECKSUM>
formulticonfiguration
generators.
Where <SOURCE_DIR_CHECKSUM>
is a checksum computed from the relativeparent directory path of the moc
input file. This scheme allows to havemoc
input files with the same name in different directories.
All not included moc
output files will be included automatically by theCMake generated file
<AUTOGEN_BUILD_DIR>/mocs_compilation.cpp
, or<AUTOGEN_BUILD_DIR>/mocs_compilation_$<CONFIG>.cpp
,
which is added to the target's sources.
Qt version detection¶
AUTOMOC
enabled targets need to know the Qt major and minorversion they're working with. The major version usually is provided by theINTERFACE_QT_MAJOR_VERSION
property of the Qt[45]Core
library,that the target links to. To find the minor version, CMake builds a list ofavailable Qt versions from
Qt5Core_VERSION_MAJOR
andQt5Core_VERSION_MINOR
variables(usually set byfind_package(Qt5...)
)Qt5Core_VERSION_MAJOR
andQt5Core_VERSION_MINOR
directory propertiesQT_VERSION_MAJOR
andQT_VERSION_MINOR
variables(usually set byfind_package(Qt4...)
)QT_VERSION_MAJOR
andQT_VERSION_MINOR
directory properties
in the context of the add_executable()
or add_library()
call.
Assumed INTERFACE_QT_MAJOR_VERSION
is a valid number, the firstentry in the list with a matching major version is taken. If no matching majorversion was found, an error is generated.If INTERFACE_QT_MAJOR_VERSION
is not a valid number, the firstentry in the list is taken.
A find_package(Qt[45]...)
call sets the QT/Qt5Core_VERSION_MAJOR/MINOR
variables. If the call is in a different context than theadd_executable()
or add_library()
call, e.g. in a function,then the version variables might not be available to the AUTOMOC
enabled target.In that case the version variables can be forwarded from thefind_package(Qt[45]...)
calling context to the add_executable()
or add_library()
calling context as directory properties.The following Qt5 example demonstrates the procedure.
Modifiers¶
AUTOMOC_EXECUTABLE
:The moc
executable will be detected automatically, but can be forced toa certain binary using this target property.
AUTOMOC_MOC_OPTIONS
:Additional command line options for moc
can be set in this target property.
AUTOMOC_MACRO_NAMES
:This list of Qt macro names can be extended to search for additional macros inheaders and sources.
AUTOMOC_DEPEND_FILTERS
:moc
dependency file names can be extracted from headers or sources bydefining file name filters in this target property.
Autouic Cmake
AUTOMOC_COMPILER_PREDEFINES
:Compiler pre definitions for moc
are written to the moc_predefs.h
file.The generation of this file can be enabled or disabled in this target property.
SKIP_AUTOMOC
:Sources and headers can be excluded from AUTOMOC
processing bysetting this source file property.
SKIP_AUTOGEN
:Source files can be excluded from AUTOMOC
,AUTOUIC
and AUTORCC
processing bysetting this source file property.
AUTOGEN_SOURCE_GROUP
:This global property can be used to group files generated byAUTOMOC
or AUTORCC
together in an IDE, e.g. in MSVS.
AUTOGEN_TARGETS_FOLDER
:This global property can be used to group AUTOMOC
,AUTOUIC
and AUTORCC
targets together in an IDE,e.g. in MSVS.
CMAKE_GLOBAL_AUTOGEN_TARGET
:A global autogen
target, that depends on all AUTOMOC
orAUTOUIC
generated <ORIGIN>_autogen
targets in the project,will be generated when this variable is ON
.
AUTOGEN_PARALLEL
:This target property controls the number of moc
or uic
processes tostart in parallel during builds.
See the cmake-qt(7)
manual for more information on using CMakewith Qt.