Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Help / command / target_compile_definitions.rst
1 target_compile_definitions
2 --------------------------
3
4 Add compile definitions to a target.
5
6 .. code-block:: cmake
7
8   target_compile_definitions(<target>
9     <INTERFACE|PUBLIC|PRIVATE> [items1...]
10     [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
11
12 Specifies compile definitions to use when compiling a given ``<target>``.  The
13 named ``<target>`` must have been created by a command such as
14 :command:`add_executable` or :command:`add_library` and must not be an
15 :ref:`ALIAS target <Alias Targets>`.
16
17 The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
18 specify the :ref:`scope <Target Usage Requirements>` of the following arguments.
19 ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`COMPILE_DEFINITIONS`
20 property of ``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
21 :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` property of ``<target>``.
22 The following arguments specify compile definitions.  Repeated calls for the
23 same ``<target>`` append items in the order called.
24
25 .. versionadded:: 3.11
26   Allow setting ``INTERFACE`` items on :ref:`IMPORTED targets <Imported Targets>`.
27
28 Arguments to ``target_compile_definitions`` may use "generator expressions"
29 with the syntax ``$<...>``.  See the :manual:`cmake-generator-expressions(7)`
30 manual for available expressions.  See the :manual:`cmake-buildsystem(7)`
31 manual for more on defining buildsystem properties.
32
33 Any leading ``-D`` on an item will be removed.  Empty items are ignored.
34 For example, the following are all equivalent:
35
36 .. code-block:: cmake
37
38   target_compile_definitions(foo PUBLIC FOO)
39   target_compile_definitions(foo PUBLIC -DFOO)  # -D removed
40   target_compile_definitions(foo PUBLIC "" FOO) # "" ignored
41   target_compile_definitions(foo PUBLIC -D FOO) # -D becomes "", then ignored
42
43 Definitions may optionally have values:
44
45 .. code-block:: cmake
46
47   target_compile_definitions(foo PUBLIC FOO=1)
48
49 Note that many compilers treat ``-DFOO`` as equivalent to ``-DFOO=1``, but
50 other tools may not recognize this in all circumstances (e.g. IntelliSense).