target_sources.rst 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. target_sources
  2. --------------
  3. .. versionadded:: 3.1
  4. Add sources to a target.
  5. .. code-block:: cmake
  6. target_sources(<target>
  7. <INTERFACE|PUBLIC|PRIVATE> [items1...]
  8. [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
  9. Specifies sources to use when building a target and/or its dependents.
  10. Relative source file paths are interpreted as being relative to the current
  11. source directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). The
  12. named ``<target>`` must have been created by a command such as
  13. :command:`add_executable` or :command:`add_library` or
  14. :command:`add_custom_target` and must not be an
  15. :ref:`ALIAS target <Alias Targets>`.
  16. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
  17. specify the scope of the items following them. ``PRIVATE`` and ``PUBLIC``
  18. items will populate the :prop_tgt:`SOURCES` property of
  19. ``<target>``, which are used when building the target itself.
  20. ``PUBLIC`` and ``INTERFACE`` items will populate the
  21. :prop_tgt:`INTERFACE_SOURCES` property of ``<target>``, which are used
  22. when building dependents. (:ref:`IMPORTED targets <Imported Targets>`
  23. only support ``INTERFACE`` items because they are not build targets.)
  24. The following arguments specify sources. Repeated calls for the same
  25. ``<target>`` append items in the order called. The targets created by
  26. :command:`add_custom_target` can only have ``PRIVATE`` scope.
  27. Arguments to ``target_sources`` may use "generator expressions"
  28. with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  29. manual for available expressions. See the :manual:`cmake-buildsystem(7)`
  30. manual for more on defining buildsystem properties.
  31. See also the :policy:`CMP0076` policy for older behavior related to the
  32. handling of relative source file paths.