6 Add sources to a target.
10 target_sources(<target>
11 <INTERFACE|PUBLIC|PRIVATE> [items1...]
12 [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
14 Specifies sources to use when building a target and/or its dependents.
15 The named ``<target>`` must have been created by a command such as
16 :command:`add_executable` or :command:`add_library` or
17 :command:`add_custom_target` and must not be an
18 :ref:`ALIAS target <Alias Targets>`. The ``<items>`` may use
19 :manual:`generator expressions <cmake-generator-expressions(7)>`.
21 .. versionadded:: 3.20
22 ``<target>`` can be a custom target.
24 The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
25 specify the :ref:`scope <Target Usage Requirements>` of the source file paths
26 (``<items>``) that follow them. ``PRIVATE`` and ``PUBLIC`` items will
27 populate the :prop_tgt:`SOURCES` property of ``<target>``, which are used when
28 building the target itself. ``PUBLIC`` and ``INTERFACE`` items will populate the
29 :prop_tgt:`INTERFACE_SOURCES` property of ``<target>``, which are used
30 when building dependents. A target created by :command:`add_custom_target`
31 can only have ``PRIVATE`` scope.
33 Repeated calls for the same ``<target>`` append items in the order called.
36 Allow exporting targets with :prop_tgt:`INTERFACE_SOURCES`.
38 .. versionadded:: 3.11
39 Allow setting ``INTERFACE`` items on
40 :ref:`IMPORTED targets <Imported Targets>`.
42 .. versionchanged:: 3.13
43 Relative source file paths are interpreted as being relative to the current
44 source directory (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`).
45 See policy :policy:`CMP0076`.
47 A path that begins with a generator expression is left unmodified.
48 When a target's :prop_tgt:`SOURCE_DIR` property differs from
49 :variable:`CMAKE_CURRENT_SOURCE_DIR`, use absolute paths in generator
50 expressions to ensure the sources are correctly assigned to the target.
54 # WRONG: starts with generator expression, but relative path used
55 target_sources(MyTarget PRIVATE "$<$<CONFIG:Debug>:dbgsrc.cpp>")
57 # CORRECT: absolute path used inside the generator expression
58 target_sources(MyTarget PRIVATE "$<$<CONFIG:Debug>:${CMAKE_CURRENT_SOURCE_DIR}/dbgsrc.cpp>")
60 See the :manual:`cmake-buildsystem(7)` manual for more on defining
61 buildsystem properties.
66 .. versionadded:: 3.23
70 target_sources(<target>
71 [<INTERFACE|PUBLIC|PRIVATE>
72 [FILE_SET <set> [TYPE <type>] [BASE_DIRS <dirs>...] [FILES <files>...]]...
75 Adds a file set to a target, or adds files to an existing file set. Targets
76 have zero or more named file sets. Each file set has a name, a type, a scope of
77 ``INTERFACE``, ``PUBLIC``, or ``PRIVATE``, one or more base directories, and
78 files within those directories. The acceptable types include:
82 Sources intended to be used via a language's ``#include`` mechanism.
88 Experimental. Gated by ``CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API``
90 Sources which contain C++ interface module or partition units (i.e., those
91 using the ``export`` keyword). This file set type may not have an
92 ``INTERFACE`` scope except on ``IMPORTED`` targets.
94 ``CXX_MODULE_HEADER_UNITS``
98 Experimental. Gated by ``CMAKE_EXPERIMENTAL_CXX_MODULE_CMAKE_API``
100 C++ header sources which may be imported by other C++ source code. This file
101 set type may not have an ``INTERFACE`` scope except on ``IMPORTED`` targets.
103 The optional default file sets are named after their type. The target may not
104 be a custom target or :prop_tgt:`FRAMEWORK` target.
106 Files in a ``PRIVATE`` or ``PUBLIC`` file set are marked as source files for
107 the purposes of IDE integration. Additionally, files in ``HEADERS`` file sets
108 have their :prop_sf:`HEADER_FILE_ONLY` property set to ``TRUE``. Files in an
109 ``INTERFACE`` or ``PUBLIC`` file set can be installed with the
110 :command:`install(TARGETS)` command, and exported with the
111 :command:`install(EXPORT)` and :command:`export` commands.
113 Each ``target_sources(FILE_SET)`` entry starts with ``INTERFACE``, ``PUBLIC``, or
114 ``PRIVATE`` and accepts the following arguments:
118 The name of the file set to create or add to. It must contain only letters,
119 numbers and underscores. Names starting with a capital letter are reserved
120 for built-in file sets predefined by CMake. The only predefined set names
121 are those matching the acceptable types. All other set names must not start
122 with a capital letter or
127 Every file set is associated with a particular type of file. Only types
128 specified above may be used and it is an error to specify anything else. As
129 a special case, if the name of the file set is one of the types, the type
130 does not need to be specified and the ``TYPE <type>`` arguments can be
131 omitted. For all other file set names, ``TYPE`` is required.
133 ``BASE_DIRS <dirs>...``
135 An optional list of base directories of the file set. Any relative path
136 is treated as relative to the current source directory
137 (i.e. :variable:`CMAKE_CURRENT_SOURCE_DIR`). If no ``BASE_DIRS`` are
138 specified when the file set is first created, the value of
139 :variable:`CMAKE_CURRENT_SOURCE_DIR` is added. This argument supports
140 :manual:`generator expressions <cmake-generator-expressions(7)>`.
142 No two base directories for a file set may be sub-directories of each other.
143 This requirement must be met across all base directories added to a file set,
144 not just those within a single call to ``target_sources()``.
148 An optional list of files to add to the file set. Each file must be in
149 one of the base directories, or a subdirectory of one of the base
150 directories. This argument supports
151 :manual:`generator expressions <cmake-generator-expressions(7)>`.
153 If relative paths are specified, they are considered relative to
154 :variable:`CMAKE_CURRENT_SOURCE_DIR` at the time ``target_sources()`` is
155 called. An exception to this is a path starting with ``$<``. Such paths
156 are treated as relative to the target's source directory after evaluation
157 of generator expressions.
159 The following target properties are set by ``target_sources(FILE_SET)``,
160 but they should not generally be manipulated directly:
162 For file sets of type ``HEADERS``:
164 * :prop_tgt:`HEADER_SETS`
165 * :prop_tgt:`INTERFACE_HEADER_SETS`
166 * :prop_tgt:`HEADER_SET`
167 * :prop_tgt:`HEADER_SET_<NAME>`
168 * :prop_tgt:`HEADER_DIRS`
169 * :prop_tgt:`HEADER_DIRS_<NAME>`
171 For file sets of type ``CXX_MODULES``:
173 * :prop_tgt:`CXX_MODULE_SETS`
174 * :prop_tgt:`INTERFACE_CXX_MODULE_SETS`
175 * :prop_tgt:`CXX_MODULE_SET`
176 * :prop_tgt:`CXX_MODULE_SET_<NAME>`
177 * :prop_tgt:`CXX_MODULE_DIRS`
178 * :prop_tgt:`CXX_MODULE_DIRS_<NAME>`
180 For file sets of type ``CXX_MODULE_HEADER_UNITS``:
182 * :prop_tgt:`CXX_MODULE_HEADER_UNIT_SETS`
183 * :prop_tgt:`INTERFACE_CXX_MODULE_HEADER_UNIT_SETS`
184 * :prop_tgt:`CXX_MODULE_HEADER_UNIT_SET`
185 * :prop_tgt:`CXX_MODULE_HEADER_UNIT_SET_<NAME>`
186 * :prop_tgt:`CXX_MODULE_HEADER_UNIT_DIRS`
187 * :prop_tgt:`CXX_MODULE_HEADER_UNIT_DIRS_<NAME>`
189 Target properties related to include directories are also modified by
190 ``target_sources(FILE_SET)`` as follows:
192 :prop_tgt:`INCLUDE_DIRECTORIES`
194 If the ``TYPE`` is ``HEADERS`` or ``CXX_MODULE_HEADER_UNITS``, and the scope
195 of the file set is ``PRIVATE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of
196 the file set are wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this
199 :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
201 If the ``TYPE`` is ``HEADERS`` or ``CXX_MODULE_HEADER_UNITS``, and the scope
202 of the file set is ``INTERFACE`` or ``PUBLIC``, all of the ``BASE_DIRS`` of
203 the file set are wrapped in :genex:`$<BUILD_INTERFACE>` and appended to this