target_include_directories.rst 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. target_include_directories
  2. --------------------------
  3. Add include directories to a target.
  4. .. code-block:: cmake
  5. target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
  6. <INTERFACE|PUBLIC|PRIVATE> [items1...]
  7. [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
  8. Specifies include directories to use when compiling a given target.
  9. The named ``<target>`` must have been created by a command such
  10. as :command:`add_executable` or :command:`add_library` and must not be an
  11. :ref:`ALIAS target <Alias Targets>`.
  12. By using ``AFTER`` or ``BEFORE`` explicitly, you can select between appending
  13. and prepending, independent of the default.
  14. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to specify
  15. the :ref:`scope <Target Usage Requirements>` of the following arguments.
  16. ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`INCLUDE_DIRECTORIES`
  17. property of ``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
  18. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` property of ``<target>``.
  19. The following arguments specify include directories.
  20. .. versionadded:: 3.11
  21. Allow setting ``INTERFACE`` items on :ref:`IMPORTED targets <Imported Targets>`.
  22. Repeated calls for the same ``<target>`` append items in the order called.
  23. If ``SYSTEM`` is specified, the compiler will be told the directories
  24. are meant as system include directories on some platforms. This may
  25. have effects such as suppressing warnings or skipping the contained
  26. headers in dependency calculations (see compiler documentation).
  27. Additionally, system include directories are searched after normal
  28. include directories regardless of the order specified.
  29. If ``SYSTEM`` is used together with ``PUBLIC`` or ``INTERFACE``, the
  30. :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` target property will be
  31. populated with the specified directories.
  32. Arguments to ``target_include_directories`` may use "generator expressions"
  33. with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  34. manual for available expressions. See the :manual:`cmake-buildsystem(7)`
  35. manual for more on defining buildsystem properties.
  36. Specified include directories may be absolute paths or relative paths.
  37. A relative path will be interpreted as relative to the current source
  38. directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and converted to an
  39. absolute path before storing it in the associated target property.
  40. If the path starts with a generator expression, it will always be assumed
  41. to be an absolute path (with one exception noted below) and will be used
  42. unmodified.
  43. Include directories usage requirements commonly differ between the build-tree
  44. and the install-tree. The :genex:`BUILD_INTERFACE` and
  45. :genex:`INSTALL_INTERFACE` generator expressions can be used to describe
  46. separate usage requirements based on the usage location. Relative paths
  47. are allowed within the :genex:`INSTALL_INTERFACE` expression and are
  48. interpreted as relative to the installation prefix. Relative paths should not
  49. be used in :genex:`BUILD_INTERFACE` expressions because they will not be
  50. converted to absolute. For example:
  51. .. code-block:: cmake
  52. target_include_directories(mylib PUBLIC
  53. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/mylib>
  54. $<INSTALL_INTERFACE:include/mylib> # <prefix>/include/mylib
  55. )
  56. Creating Relocatable Packages
  57. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  58. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  59. .. include:: /include/INTERFACE_INCLUDE_DIRECTORIES_WARNING.txt