Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qt_wrap_cpp.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qt-wrap-cpp.html
6\ingroup cmake-commands-qtcore
7
8\title qt_wrap_cpp
9\keyword qt6_wrap_cpp
10
11\summary {Creates .moc files from sources.}
12
13\include cmake-find-package-core.qdocinc
14
15\cmakecommandsince 5.0
16
17\section1 Synopsis
18
19\badcode
20qt_wrap_cpp(<VAR> src_file1 [src_file2 ...]
21 [TARGET target]
22 [OPTIONS ...]
23 [DEPENDS ...])
24\endcode
25
26\versionlessCMakeCommandsNote qt6_wrap_cpp()
27
28\section1 Description
29
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>}.
33
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}.
36
37\section1 Arguments
38
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}.
42
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>}.
48
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}.
51
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
58them.
59
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.
64
65\section1 Examples
66
67\snippet cmake-macros/examples.cmake qt_wrap_cpp_1
68
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.
72
73\snippet cmake-macros/examples.cmake qt_wrap_cpp_2
74
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
77\c{OPTIONS}.
78
79\snippet cmake-macros/examples.cmake qt_wrap_cpp_4
80
81\snippet cmake-macros/examples.cpp qt_wrap_cpp_4
82
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.
88
89\snippet cmake-macros/examples.cmake qt_wrap_cpp_4
90*/