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