1CMAKE-QT(7) CMake CMAKE-QT(7)
2
3
4
6 cmake-qt - CMake Qt Features Reference
7
9 CMake can find and use Qt 4 and Qt 5 libraries. The Qt 4 libraries are
10 found by the FindQt4 find-module shipped with CMake, whereas the Qt 5
11 libraries are found using "Config-file Packages" shipped with Qt 5. See
12 cmake-packages(7) for more information about CMake packages, and see
13 the Qt cmake manual for your Qt version.
14
15 Qt 4 and Qt 5 may be used together in the same CMake buildsystem:
16
17 cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)
18
19 project(Qt4And5)
20
21 set(CMAKE_AUTOMOC ON)
22
23 find_package(Qt5 COMPONENTS Widgets DBus REQUIRED)
24 add_executable(publisher publisher.cpp)
25 target_link_libraries(publisher Qt5::Widgets Qt5::DBus)
26
27 find_package(Qt4 REQUIRED)
28 add_executable(subscriber subscriber.cpp)
29 target_link_libraries(subscriber Qt4::QtGui Qt4::QtDBus)
30
31 A CMake target may not link to both Qt 4 and Qt 5. A diagnostic is is‐
32 sued if this is attempted or results from transitive target dependency
33 evaluation.
34
36 Qt relies on some bundled tools for code generation, such as moc for
37 meta-object code generation, uic for widget layout and population, and
38 rcc for virtual file system content generation. These tools may be au‐
39 tomatically invoked by cmake(1) if the appropriate conditions are met.
40 The automatic tool invocation may be used with both Qt 4 and Qt 5.
41
42 AUTOMOC
43 The AUTOMOC target property controls whether cmake(1) inspects the C++
44 files in the target to determine if they require moc to be run, and to
45 create rules to execute moc at the appropriate time.
46
47 If a macro from AUTOMOC_MACRO_NAMES is found in a header file, moc will
48 be run on the file. The result will be put into a file named according
49 to moc_<basename>.cpp. If the macro is found in a C++ implementation
50 file, the moc output will be put into a file named according to <base‐
51 name>.moc, following the Qt conventions. The <basename>.moc must be
52 included by the user in the C++ implementation file with a preprocessor
53 #include.
54
55 Included moc_*.cpp and *.moc files will be generated in the <AUTO‐
56 GEN_BUILD_DIR>/include directory which is automatically added to the
57 target's INCLUDE_DIRECTORIES.
58
59 • This differs from CMake 3.7 and below; see their documentation for
60 details.
61
62 • For multi configuration generators, the include directory is <AUTO‐
63 GEN_BUILD_DIR>/include_<CONFIG>.
64
65 • See AUTOGEN_BUILD_DIR.
66
67 Not included moc_<basename>.cpp files will be generated in custom fold‐
68 ers to avoid name collisions and included in a separate file which is
69 compiled into the target, named either <AUTOGEN_BUILD_DIR>/mocs_compi‐
70 lation.cpp or <AUTOGEN_BUILD_DIR>/mocs_compilation_$<CONFIG>.cpp.
71
72 • See AUTOGEN_BUILD_DIR.
73
74 The moc command line will consume the COMPILE_DEFINITIONS and IN‐
75 CLUDE_DIRECTORIES target properties from the target it is being invoked
76 for, and for the appropriate build configuration.
77
78 The AUTOMOC target property may be pre-set for all following targets by
79 setting the CMAKE_AUTOMOC variable. The AUTOMOC_MOC_OPTIONS target
80 property may be populated to set options to pass to moc. The CMAKE_AU‐
81 TOMOC_MOC_OPTIONS variable may be populated to pre-set the options for
82 all following targets.
83
84 Additional macro names to search for can be added to AUTO‐
85 MOC_MACRO_NAMES.
86
87 Additional moc dependency file names can be extracted from source code
88 by using AUTOMOC_DEPEND_FILTERS.
89
90 Source C++ files can be excluded from AUTOMOC processing by enabling
91 SKIP_AUTOMOC or the broader SKIP_AUTOGEN.
92
93 AUTOUIC
94 The AUTOUIC target property controls whether cmake(1) inspects the C++
95 files in the target to determine if they require uic to be run, and to
96 create rules to execute uic at the appropriate time.
97
98 If a preprocessor #include directive is found which matches
99 <path>ui_<basename>.h, and a <basename>.ui file exists, then uic will
100 be executed to generate the appropriate file. The <basename>.ui file
101 is searched for in the following places
102
103 1. <source_dir>/<basename>.ui
104
105 2. <source_dir>/<path><basename>.ui
106
107 3. <AUTOUIC_SEARCH_PATHS>/<basename>.ui
108
109 4. <AUTOUIC_SEARCH_PATHS>/<path><basename>.ui
110
111 where <source_dir> is the directory of the C++ file and
112 AUTOUIC_SEARCH_PATHS is a list of additional search paths.
113
114 The generated generated ui_*.h files are placed in the <AUTO‐
115 GEN_BUILD_DIR>/include directory which is automatically added to the
116 target's INCLUDE_DIRECTORIES.
117
118 • This differs from CMake 3.7 and below; see their documentation for
119 details.
120
121 • For multi configuration generators, the include directory is <AUTO‐
122 GEN_BUILD_DIR>/include_<CONFIG>.
123
124 • See AUTOGEN_BUILD_DIR.
125
126 The AUTOUIC target property may be pre-set for all following targets by
127 setting the CMAKE_AUTOUIC variable. The AUTOUIC_OPTIONS target prop‐
128 erty may be populated to set options to pass to uic. The
129 CMAKE_AUTOUIC_OPTIONS variable may be populated to pre-set the options
130 for all following targets. The AUTOUIC_OPTIONS source file property
131 may be set on the <basename>.ui file to set particular options for the
132 file. This overrides options from the AUTOUIC_OPTIONS target property.
133
134 A target may populate the INTERFACE_AUTOUIC_OPTIONS target property
135 with options that should be used when invoking uic. This must be con‐
136 sistent with the AUTOUIC_OPTIONS target property content of the depen‐
137 der target. The CMAKE_DEBUG_TARGET_PROPERTIES variable may be used to
138 track the origin target of such INTERFACE_AUTOUIC_OPTIONS. This means
139 that a library which provides an alternative translation system for Qt
140 may specify options which should be used when running uic:
141
142 add_library(KI18n klocalizedstring.cpp)
143 target_link_libraries(KI18n Qt5::Core)
144
145 # KI18n uses the tr2i18n() function instead of tr(). That function is
146 # declared in the klocalizedstring.h header.
147 set(autouic_options
148 -tr tr2i18n
149 -include klocalizedstring.h
150 )
151
152 set_property(TARGET KI18n APPEND PROPERTY
153 INTERFACE_AUTOUIC_OPTIONS ${autouic_options}
154 )
155
156 A consuming project linking to the target exported from upstream auto‐
157 matically uses appropriate options when uic is run by AUTOUIC, as a re‐
158 sult of linking with the IMPORTED target:
159
160 set(CMAKE_AUTOUIC ON)
161 # Uses a libwidget.ui file:
162 add_library(LibWidget libwidget.cpp)
163 target_link_libraries(LibWidget
164 KF5::KI18n
165 Qt5::Widgets
166 )
167
168 Source files can be excluded from AUTOUIC processing by enabling
169 SKIP_AUTOUIC or the broader SKIP_AUTOGEN.
170
171 AUTORCC
172 The AUTORCC target property controls whether cmake(1) creates rules to
173 execute rcc at the appropriate time on source files which have the suf‐
174 fix .qrc.
175
176 add_executable(myexe main.cpp resource_file.qrc)
177
178 The AUTORCC target property may be pre-set for all following targets by
179 setting the CMAKE_AUTORCC variable. The AUTORCC_OPTIONS target prop‐
180 erty may be populated to set options to pass to rcc. The CMAKE_AU‐
181 TORCC_OPTIONS variable may be populated to pre-set the options for all
182 following targets. The AUTORCC_OPTIONS source file property may be set
183 on the <name>.qrc file to set particular options for the file. This
184 overrides options from the AUTORCC_OPTIONS target property.
185
186 Source files can be excluded from AUTORCC processing by enabling
187 SKIP_AUTORCC or the broader SKIP_AUTOGEN.
188
190 The moc and uic tools are executed as part of a synthesized <ORI‐
191 GIN>_autogen custom target generated by CMake. By default that <ORI‐
192 GIN>_autogen target inherits the dependencies of the <ORIGIN> target
193 (see AUTOGEN_ORIGIN_DEPENDS). Target dependencies may be added to the
194 <ORIGIN>_autogen target by adding them to the AUTOGEN_TARGET_DEPENDS
195 target property.
196
198 When using the Visual Studio generators, CMake generates a PRE_BUILD
199 custom command instead of the <ORIGIN>_autogen custom target (for AUTO‐
200 MOC and AUTOUIC). This isn't always possible though and an <ORI‐
201 GIN>_autogen custom target is used, when either
202
203 • the <ORIGIN> target depends on GENERATED files which aren't excluded
204 from AUTOMOC and AUTOUIC by SKIP_AUTOMOC, SKIP_AUTOUIC, SKIP_AUTOGEN
205 or CMP0071
206
207 • AUTOGEN_TARGET_DEPENDS lists a source file
208
209 • CMAKE_GLOBAL_AUTOGEN_TARGET is enabled
210
212 The Qt 4 and 5 IMPORTED targets for the QtGui libraries specify that
213 the qtmain.lib static library shipped with Qt will be linked by all de‐
214 pendent executables which have the WIN32_EXECUTABLE enabled.
215
216 To disable this behavior, enable the Qt5_NO_LINK_QTMAIN target property
217 for Qt 5 based targets or QT4_NO_LINK_QTMAIN target property for Qt 4
218 based targets.
219
220 add_executable(myexe WIN32 main.cpp)
221 target_link_libraries(myexe Qt4::QtGui)
222
223 add_executable(myexe_no_qtmain WIN32 main_no_qtmain.cpp)
224 set_property(TARGET main_no_qtmain PROPERTY QT4_NO_LINK_QTMAIN ON)
225 target_link_libraries(main_no_qtmain Qt4::QtGui)
226
228 2000-2022 Kitware, Inc. and Contributors
229
230
231
232
2333.22.2 Jan 25, 2022 CMAKE-QT(7)