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