cmake-qt.7.rst 11 KB

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