target_compile_definitions.rst 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 :ref:`scope <Target Usage Requirements>` of the following arguments.
  14. ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`COMPILE_DEFINITIONS`
  15. property of ``<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. .. |command_name| replace:: ``target_compile_definitions``
  22. .. include:: GENEX_NOTE.txt
  23. Any leading ``-D`` on an item will be removed. Empty items are ignored.
  24. For example, the following are all equivalent:
  25. .. code-block:: cmake
  26. target_compile_definitions(foo PUBLIC FOO)
  27. target_compile_definitions(foo PUBLIC -DFOO) # -D removed
  28. target_compile_definitions(foo PUBLIC "" FOO) # "" ignored
  29. target_compile_definitions(foo PUBLIC -D FOO) # -D becomes "", then ignored
  30. Definitions may optionally have values:
  31. .. code-block:: cmake
  32. target_compile_definitions(foo PUBLIC FOO=1)
  33. Note that many compilers treat ``-DFOO`` as equivalent to ``-DFOO=1``, but
  34. other tools may not recognize this in all circumstances (e.g. IntelliSense).
  35. See Also
  36. ^^^^^^^^
  37. * :command:`add_compile_definitions`
  38. * :command:`target_compile_features`
  39. * :command:`target_compile_options`
  40. * :command:`target_include_directories`
  41. * :command:`target_link_libraries`
  42. * :command:`target_link_directories`
  43. * :command:`target_link_options`
  44. * :command:`target_precompile_headers`
  45. * :command:`target_sources`