target_compile_definitions.rst 2.0 KB

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