Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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\c{qt_wrap_cpp} supports two signatures. The first signature was added in Qt 6.8
20and has the following form:
21
22\badcode
23qt_wrap_cpp(<TARGET> src_file1 [src_file2 ...]
24 [OPTIONS ...]
25 [DEPENDS ...])
26\endcode
27
28\note The signature above is recommended over the older signature.
29
30\badcode
31qt_wrap_cpp(<VAR> src_file1 [src_file2 ...]
32 [TARGET target]
33 [OPTIONS ...]
34 [DEPENDS ...])
35\endcode
36
37\versionlessCMakeCommandsNote qt6_wrap_cpp()
38
39\section1 Description
40
41Creates rules for calling the \l{moc}{Meta-Object Compiler (moc)} on the given
42source files. For each input file, an output file is generated in the build
43directory. The paths of the generated files are added to \c{<VAR>}.
44
45\note This is a low-level macro. See the \l{CMake AUTOMOC Documentation} for a
46more convenient way to let source files be processed with \c{moc}.
47
48\section1 Arguments
49
50You can set an explicit \c{TARGET}. This will make sure that the target
51properties \c{INCLUDE_DIRECTORIES} and \c{COMPILE_DEFINITIONS} are also used
52when scanning the source files with \c{moc}.
53
54Since Qt 6.8, when a source file is passed to \c{qt_wrap_cpp} instead of a
55header file to generate a \c{.moc} file for a target, the \c{TARGET} parameter
56is needed to set the correct include path for the generated \c{.moc} file in
57the source file. As generated \c{.moc} files are added to the target's
58sources by \c{qt_wrap_cpp}, they are not added to \c{<VAR>}.
59
60You can set additional \c{OPTIONS} that should be added to the \c{moc} calls.
61You can find possible options in the \l{moc}{moc documentation}.
62
63The \c{OPTIONS} can evaluate generator expressions when \c{TARGET} is set.
64\note If the \c{OPTIONS} include both generator expressions and special
65characters, use variables to implement them. For example, use \c{$<ANGLE-R>},
66\c{$<COMMA>} and \c{$<SEMICOLON>} instead of \c{>}, \c{,} and \c{:}. Otherwise,
67the generator expression will not be evaluated correctly. \c {OPTIONS} are
68wrapped in generator expressions, so you must escape special characters in
69them.
70
71\c{DEPENDS} allows you to add additional dependencies for recreation of the
72generated files. This is useful when the sources have implicit dependencies,
73like code for a Qt plugin that includes a \c{.json} file using the
74Q_PLUGIN_METADATA() macro.
75
76\section1 Examples
77
78Since Qt 6.8:
79
80\snippet cmake-macros/examples.cmake qt_wrap_cpp_4
81\br
82\snippet cmake-macros/examples.cpp qt_wrap_cpp_4
83
84In the above file, \c{myapp.moc} is included in \c{myapp.cpp}.
85To generate the \c{myapp.moc} file, the \c{qt_wrap_cpp} macro is used with the
86\c{TARGET} parameter. The \c{.moc} file and its path will be added to the
87target's sources and include directories by the \c{qt_wrap_cpp} macro.
88
89The old version:
90\snippet cmake-macros/examples.cmake qt_wrap_cpp_1
91
92In the following example, the generator expressions passed to \c{OPTIONS}
93will be evaluated since \c{TARGET} is set. The argument is specified this way to
94avoid syntax errors in the generator expressions.
95
96\snippet cmake-macros/examples.cmake qt_wrap_cpp_2
97
98The following example uses \l{https://cmake.org/cmake/help/latest/command/target_compile_definitions.html}{target_compile_definitions}
99to set \l{https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_DEFINITIONS.html}{COMPILE_DEFINITIONS} which will be added to
100\c{OPTIONS}.
101\snippet cmake-macros/examples.cmake qt_wrap_cpp_3
102
103*/