target_precompile_headers.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. target_precompile_headers
  2. -------------------------
  3. .. versionadded:: 3.16
  4. Add a list of header files to precompile.
  5. Precompiling header files can speed up compilation by creating a partially
  6. processed version of some header files, and then using that version during
  7. compilations rather than repeatedly parsing the original headers.
  8. Main Form
  9. ^^^^^^^^^
  10. .. code-block:: cmake
  11. target_precompile_headers(<target>
  12. <INTERFACE|PUBLIC|PRIVATE> [header1...]
  13. [<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
  14. The command adds header files to the :prop_tgt:`PRECOMPILE_HEADERS` and/or
  15. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` target properties of ``<target>``.
  16. The named ``<target>`` must have been created by a command such as
  17. :command:`add_executable` or :command:`add_library` and must not be an
  18. :ref:`ALIAS target <Alias Targets>`.
  19. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
  20. specify the :ref:`scope <Target Command Scope>` of the following arguments.
  21. ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`PRECOMPILE_HEADERS`
  22. property of ``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
  23. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ``<target>``
  24. (:ref:`IMPORTED targets <Imported Targets>` only support ``INTERFACE`` items).
  25. Repeated calls for the same ``<target>`` will append items in the order called.
  26. Projects should generally avoid using ``PUBLIC`` or ``INTERFACE`` for targets
  27. that will be :command:`exported <install(EXPORT)>`, or they should at least use
  28. the :genex:`$<BUILD_INTERFACE:...>` generator expression to prevent precompile
  29. headers from appearing in an installed exported target. Consumers of a target
  30. should typically be in control of what precompile headers they use, not have
  31. precompile headers forced on them by the targets being consumed (since
  32. precompile headers are not typically usage requirements). A notable exception
  33. to this is where an :ref:`interface library <Interface Libraries>` is created
  34. to define a commonly used set of precompile headers in one place and then other
  35. targets link to that interface library privately. In this case, the interface
  36. library exists specifically to propagate the precompile headers to its
  37. consumers and the consumer is effectively still in control, since it decides
  38. whether to link to the interface library or not.
  39. The list of header files is used to generate a header file named
  40. ``cmake_pch.h|xx`` which is used to generate the precompiled header file
  41. (``.pch``, ``.gch``, ``.pchi``) artifact. The ``cmake_pch.h|xx`` header
  42. file will be force included (``-include`` for GCC, ``/FI`` for MSVC) to
  43. all source files, so sources do not need to have ``#include "pch.h"``.
  44. Header file names specified with angle brackets (e.g. ``<unordered_map>``) or
  45. explicit double quotes (escaped for the :manual:`cmake-language(7)`,
  46. e.g. ``[["other_header.h"]]``) will be treated as is, and include directories
  47. must be available for the compiler to find them. Other header file names
  48. (e.g. ``project_header.h``) are interpreted as being relative to the current
  49. source directory (e.g. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and will be
  50. included by absolute path. For example:
  51. .. code-block:: cmake
  52. target_precompile_headers(myTarget
  53. PUBLIC
  54. project_header.h
  55. PRIVATE
  56. [["other_header.h"]]
  57. <unordered_map>
  58. )
  59. .. |command_name| replace:: ``target_precompile_headers``
  60. .. |more_see_also| replace:: The :genex:`$<COMPILE_LANGUAGE:...>` generator
  61. expression is particularly useful for specifying a language-specific header
  62. to precompile for only one language (e.g. ``CXX`` and not ``C``). In this
  63. case, header file names that are not explicitly in double quotes or angle
  64. brackets must be specified by absolute path. Also, when specifying angle
  65. brackets inside a generator expression, be sure to encode the closing
  66. ``>`` as :genex:`$<ANGLE-R>`. For example:
  67. .. include:: GENEX_NOTE.txt
  68. :start-line: 1
  69. .. code-block:: cmake
  70. target_precompile_headers(mylib PRIVATE
  71. "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/cxx_only.h>"
  72. "$<$<COMPILE_LANGUAGE:C>:<stddef.h$<ANGLE-R>>"
  73. "$<$<COMPILE_LANGUAGE:CXX>:<cstddef$<ANGLE-R>>"
  74. )
  75. Reusing Precompile Headers
  76. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  77. The command also supports a second signature which can be used to specify that
  78. one target reuses a precompiled header file artifact from another target
  79. instead of generating its own:
  80. .. code-block:: cmake
  81. target_precompile_headers(<target> REUSE_FROM <other_target>)
  82. This form sets the :prop_tgt:`PRECOMPILE_HEADERS_REUSE_FROM` property to
  83. ``<other_target>`` and adds a dependency such that ``<target>`` will depend
  84. on ``<other_target>``. CMake will halt with an error if the
  85. :prop_tgt:`PRECOMPILE_HEADERS` property of ``<target>`` is already set when
  86. the ``REUSE_FROM`` form is used.
  87. .. note::
  88. The ``REUSE_FROM`` form requires the same set of compiler options,
  89. compiler flags and compiler definitions for both ``<target>`` and
  90. ``<other_target>``. Some compilers (e.g. GCC) may issue a warning if the
  91. precompiled header file cannot be used (``-Winvalid-pch``).
  92. See Also
  93. ^^^^^^^^
  94. * To disable precompile headers for specific targets, see the
  95. :prop_tgt:`DISABLE_PRECOMPILE_HEADERS` target property.
  96. * To prevent precompile headers from being used when compiling a specific
  97. source file, see the :prop_sf:`SKIP_PRECOMPILE_HEADERS` source file property.
  98. * :command:`target_compile_definitions`
  99. * :command:`target_compile_features`
  100. * :command:`target_compile_options`
  101. * :command:`target_include_directories`
  102. * :command:`target_link_libraries`
  103. * :command:`target_link_directories`
  104. * :command:`target_link_options`
  105. * :command:`target_sources`