1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
6\ingroup cmake-commands-qtcore
11\summary {Creates .moc files from sources.}
13\include cmake-find-package-core.qdocinc
20qt_wrap_cpp(<VAR> src_file1 [src_file2 ...]
26\versionlessCMakeCommandsNote qt6_wrap_cpp()
30Creates rules for calling the \l{moc}{Meta-Object Compiler (moc)} on the given
31source files. For each input file, an output file is generated in the build
32directory. The paths of the generated files are added to \c{<VAR>}.
34\note This is a low-level macro. See the \l{CMake AUTOMOC Documentation} for a
35more convenient way to let source files be processed with \c{moc}.
39You can set an explicit \c{TARGET}. This will make sure that the target
40properties \c{INCLUDE_DIRECTORIES} and \c{COMPILE_DEFINITIONS} are also used
41when scanning the source files with \c{moc}.
43Since Qt 6.8, when a source file is passed to \c{qt_wrap_cpp} instead of a
44header file to generate a \c{.moc} file for a target, the \c{TARGET} parameter
45is needed to set the correct include path for the generated \c{.moc} file in
46the source file. As generated \c{.moc} files are added to the target's
47sources by \c{qt_wrap_cpp}, they are not added to \c{<VAR>}.
49You can set additional \c{OPTIONS} that should be added to the \c{moc} calls.
50You can find possible options in the \l{moc}{moc documentation}.
52The \c{OPTIONS} can evaluate generator expressions when \c{TARGET} is set.
53\note If the \c{OPTIONS} include both generator expressions and special
54characters, use variables to implement them. For example, use \c{$<ANGLE-R>},
55\c{$<COMMA>} and \c{$<SEMICOLON>} instead of \c{>}, \c{,} and \c{:}. Otherwise,
56the generator expression will not be evaluated correctly. \c {OPTIONS} are
57wrapped in generator expressions, so you must escape special characters in
60\c{DEPENDS} allows you to add additional dependencies for recreation of the
61generated files. This is useful when the sources have implicit dependencies,
62like code for a Qt plugin that includes a \c{.json} file using the
63Q_PLUGIN_METADATA() macro.
67\snippet cmake-macros/examples.cmake qt_wrap_cpp_1
69In the following example, the generator expressions passed to \c{OPTIONS}
70will be evaluated since \c{TARGET} is set. The argument is specified this way to
71avoid syntax errors in the generator expressions.
73\snippet cmake-macros/examples.cmake qt_wrap_cpp_2
75The following example uses \l{https://cmake.org/cmake/help/latest/command/target_compile_definitions.html}{target_compile_definitions}
76to set \l{https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_DEFINITIONS.html}{COMPILE_DEFINITIONS} which will be added to
79\snippet cmake-macros/examples.cmake qt_wrap_cpp_4
81\snippet cmake-macros/examples.cpp qt_wrap_cpp_4
83In the above file, \c{myapp.moc} is included in \c{myapp.cpp}.
84To generate the \c{myapp.moc} file, the \c{qt_wrap_cpp} macro is used with the
85\c{TARGET} parameter. The first parameter is empty because the \c{.moc} file
86and its path will be added to the target's sources and include directories by
87the \c{qt_wrap_cpp} macro.
89\snippet cmake-macros/examples.cmake qt_wrap_cpp_4