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.
44
45With the target-based signature, generated files are added as sources to
46\c{<TARGET>}. With the older signature, the paths of the generated files are
47added to \c{<VAR>}.
48
49\note This is a low-level macro. See the \l{CMake AUTOMOC Documentation} for a
50more convenient way to let source files be processed with \c{moc}.
51
52\section1 Arguments
53
54You can set an explicit \c{TARGET}. This will make sure that the target
55properties \c{INCLUDE_DIRECTORIES} and \c{COMPILE_DEFINITIONS} are also used
56when scanning the source files with \c{moc}.
57
58Since Qt 6.8, when a source file is passed to \c{qt_wrap_cpp} instead of a
59header file to generate a \c{.moc} file for a target, the \c{TARGET} parameter
60is needed to set the correct include path for the generated \c{.moc} file in
61the source file.
62
63If you're calling \c qt_wrap_cpp in a directory scope that's different from the
64directory scope of the target, you must use the target-based signature of \c
65qt_wrap_cpp or pass the target with the \c TARGET argument.
66
67You can set additional \c{OPTIONS} that should be added to the \c{moc} calls.
68You can find possible options in the \l{moc}{moc documentation}.
69
70The \c{OPTIONS} can evaluate generator expressions when \c{TARGET} is set.
71\note If the \c{OPTIONS} include both generator expressions and special
72characters, use variables to implement them. For example, use \c{$<ANGLE-R>},
73\c{$<COMMA>} and \c{$<SEMICOLON>} instead of \c{>}, \c{,} and \c{:}. Otherwise,
74the generator expression will not be evaluated correctly. \c {OPTIONS} are
75wrapped in generator expressions, so you must escape special characters in
76them.
77
78\c{DEPENDS} allows you to add additional dependencies for recreation of the
79generated files. This is useful when the sources have implicit dependencies,
80like code for a Qt plugin that includes a \c{.json} file using the
81Q_PLUGIN_METADATA() macro.
82
83\section1 Examples
84
85Since Qt 6.8:
86
87\snippet cmake-macros/examples.cmake qt_wrap_cpp_4
88\br
89\snippet cmake-macros/examples.cpp qt_wrap_cpp_4
90
91In the above file, \c{myapp.moc} is included in \c{myapp.cpp}.
92To generate the \c{myapp.moc} file, the \c{qt_wrap_cpp} macro is used with the
93\c{TARGET} parameter. The \c{.moc} file and its path will be added to the
94target's sources and include directories by the \c{qt_wrap_cpp} macro.
95
96The old version:
97\snippet cmake-macros/examples.cmake qt_wrap_cpp_1
98
99In the following example, the generator expressions passed to \c{OPTIONS}
100will be evaluated since \c{TARGET} is set. The argument is specified this way to
101avoid syntax errors in the generator expressions.
102
103\snippet cmake-macros/examples.cmake qt_wrap_cpp_2
104
105The following example uses \l{https://cmake.org/cmake/help/latest/command/target_compile_definitions.html}{target_compile_definitions}
106to set \l{https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_DEFINITIONS.html}{COMPILE_DEFINITIONS} which will be added to
107\c{OPTIONS}.
108\snippet cmake-macros/examples.cmake qt_wrap_cpp_3
109
110*/