cmake-qt.7.rst 10 KB

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