target_precompile_headers.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. target_precompile_headers
  2. -------------------------
  3. Add a list of header files to precompile.
  4. .. code-block:: cmake
  5. target_precompile_headers(<target>
  6. <INTERFACE|PUBLIC|PRIVATE> [header1...]
  7. [<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
  8. target_precompile_headers(<target> REUSE_FROM <other_target>)
  9. Adds header files to :prop_tgt:`PRECOMPILE_HEADERS` or
  10. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` target properties.
  11. The second signature will reuse an already precompiled header file artefact
  12. from another target. This is done by setting the
  13. :prop_tgt:`PRECOMPILE_HEADERS_REUSE_FROM` to ``<other_target>`` value.
  14. The ``<other_target>`` will become a dependency of ``<target>``.
  15. .. note::
  16. The second signature will require the same set of compiler options,
  17. compiler flags, compiler definitions for both ``<target>``, and
  18. ``<other_target>``. Compilers (e.g. GCC) will issue a warning if the
  19. precompiled header file cannot be used (``-Winvalid-pch``).
  20. Precompiling header files can speed up compilation by creating a partially
  21. processed version of some header files, and then using that version during
  22. compilations rather than repeatedly parsing the original headers.
  23. The named ``<target>`` must have been created by a command such as
  24. :command:`add_executable` or :command:`add_library` and must not be an
  25. :ref:`ALIAS target <Alias Targets>`.
  26. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
  27. specify the scope of the following arguments. ``PRIVATE`` and ``PUBLIC``
  28. items will populate the :prop_tgt:`PRECOMPILE_HEADERS` property of
  29. ``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
  30. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ``<target>``.
  31. (:ref:`IMPORTED targets <Imported Targets>` only support ``INTERFACE`` items.)
  32. Repeated calls for the same ``<target>`` append items in the order called.
  33. Arguments to ``target_precompile_headers`` may use "generator expressions"
  34. with the syntax ``$<...>``.
  35. See the :manual:`cmake-generator-expressions(7)` manual for available
  36. expressions. See the :manual:`cmake-compile-features(7)` manual for
  37. information on compile features and a list of supported compilers.
  38. The ``$<COMPILE_LANGUAGE:...>`` generator expression is particularly
  39. useful for specifying a language-specific header to precompile for
  40. only one language (e.g. ``CXX`` and not ``C``).
  41. Usage
  42. ^^^^^
  43. .. code-block:: cmake
  44. target_precompile_headers(<target>
  45. PUBLIC
  46. project_header.h
  47. "$<$<COMPILE_LANGUAGE:CXX>:cxx_only.h>"
  48. PRIVATE
  49. [["other_header.h"]]
  50. <unordered_map>
  51. )
  52. The list of header files is used to generate a header file named
  53. ``cmake_pch.h|xx`` which is used to generate the precompiled header file
  54. (``.pch``, ``.gch``, ``.pchi``) artifact. The ``cmake_pch.h|xx`` header
  55. file will be force included (``-include`` for GCC, ``/FI`` for MSVC) to
  56. all source files, so sources do not need to have ``#include "pch.h"``.
  57. Header file names specified with angle brackets (e.g. ``<unordered_map>``) or
  58. explicit double quotes (escaped for the :manual:`cmake-language(7)`,
  59. e.g. ``[["other_header.h"]]``) will be treated as is, and include directories
  60. must be available for the compiler to find them. Other header file names
  61. (e.g. ``project_header.h``) are interpreted as being relative to the current
  62. source directory (e.g. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and will be
  63. included by absolute path.
  64. When specifying angle brackets inside a :manual:`generator expression
  65. <cmake-generator-expressions(7)>`, be sure to encode the closing ``>``
  66. as ``$<ANGLE-R>``. For example:
  67. .. code-block:: cmake
  68. target_precompile_headers(mylib PRIVATE
  69. "$<$<COMPILE_LANGUAGE:C>:<stddef.h$<ANGLE-R>>"
  70. "$<$<COMPILE_LANGUAGE:CXX>:<cstddef$<ANGLE-R>>"
  71. )
  72. See Also
  73. ^^^^^^^^
  74. For disabling precompile headers for specific targets there is the
  75. property :prop_tgt:`DISABLE_PRECOMPILE_HEADERS`.
  76. For skipping certain source files there is the source file property
  77. :prop_sf:`SKIP_PRECOMPILE_HEADERS`.