target_include_directories.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 Command Scope>` 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. .. |command_name| replace:: ``target_include_directories``
  33. .. include:: GENEX_NOTE.txt
  34. Specified include directories may be absolute paths or relative paths.
  35. A relative path will be interpreted as relative to the current source
  36. directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and converted to an
  37. absolute path before storing it in the associated target property.
  38. If the path starts with a generator expression, it will always be assumed
  39. to be an absolute path (with one exception noted below) and will be used
  40. unmodified.
  41. Include directories usage requirements commonly differ between the build-tree
  42. and the install-tree. The :genex:`BUILD_INTERFACE` and
  43. :genex:`INSTALL_INTERFACE` generator expressions can be used to describe
  44. separate usage requirements based on the usage location. Relative paths
  45. are allowed within the :genex:`INSTALL_INTERFACE` expression and are
  46. interpreted as relative to the installation prefix. Relative paths should not
  47. be used in :genex:`BUILD_INTERFACE` expressions because they will not be
  48. converted to absolute. For example:
  49. .. code-block:: cmake
  50. target_include_directories(mylib PUBLIC
  51. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/mylib>
  52. $<INSTALL_INTERFACE:include/mylib> # <prefix>/include/mylib
  53. )
  54. Creating Relocatable Packages
  55. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  56. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  57. .. include:: /include/INTERFACE_INCLUDE_DIRECTORIES_WARNING.txt
  58. See Also
  59. ^^^^^^^^
  60. * :command:`include_directories`
  61. * :command:`target_compile_definitions`
  62. * :command:`target_compile_features`
  63. * :command:`target_compile_options`
  64. * :command:`target_link_libraries`
  65. * :command:`target_link_directories`
  66. * :command:`target_link_options`
  67. * :command:`target_precompile_headers`
  68. * :command:`target_sources`