target_precompile_headers.rst 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Usage
  39. ^^^^^
  40. .. code-block:: cmake
  41. target_precompile_headers(<target>
  42. PUBLIC
  43. project_header.h
  44. PRIVATE
  45. [["other_header.h"]]
  46. <unordered_map>
  47. )
  48. The list of header files is used to generate a header file named
  49. ``cmake_pch.h|xx`` which is used to generate the precompiled header file
  50. (``.pch``, ``.gch``, ``.pchi``) artifact. The ``cmake_pch.h|xx`` header
  51. file will be force included (``-include`` for GCC, ``/FI`` for MSVC) to
  52. all source files, so sources do not need to have ``#include "pch.h"``.
  53. Header file names specified with angle brackets (e.g. ``<unordered_map>``) or
  54. explicit double quotes (escaped for the :manual:`cmake-language(7)`,
  55. e.g. ``[["other_header.h"]]``) will be treated as is, and include directories
  56. must be available for the compiler to find them. Other header file names
  57. (e.g. ``project_header.h``) are interpreted as being relative to the current
  58. source directory (e.g. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and will be
  59. included by absolute path.
  60. See Also
  61. ^^^^^^^^
  62. For disabling precompile headers for specific targets there is the
  63. property :prop_tgt:`DISABLE_PRECOMPILE_HEADERS`.
  64. For skipping certain source files there is the source file property
  65. :prop_sf:`SKIP_PRECOMPILE_HEADERS`.