target_compile_options.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. target_compile_options
  2. ----------------------
  3. Add compile options to a target.
  4. .. code-block:: cmake
  5. target_compile_options(<target> [BEFORE]
  6. <INTERFACE|PUBLIC|PRIVATE> [items1...]
  7. [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
  8. Adds options to the :prop_tgt:`COMPILE_OPTIONS` or
  9. :prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties. These options
  10. are used when compiling the given ``<target>``, which must have been
  11. created by a command such as :command:`add_executable` or
  12. :command:`add_library` and must not be an :ref:`ALIAS target <Alias Targets>`.
  13. .. note::
  14. These options are not used when linking the target.
  15. See the :command:`target_link_options` command for that.
  16. Arguments
  17. ^^^^^^^^^
  18. If ``BEFORE`` is specified, the content will be prepended to the property
  19. instead of being appended. See policy :policy:`CMP0101` which affects
  20. whether ``BEFORE`` will be ignored in certain cases.
  21. The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
  22. specify the :ref:`scope <Target Usage Requirements>` of the following arguments.
  23. ``PRIVATE`` and ``PUBLIC`` items will populate the :prop_tgt:`COMPILE_OPTIONS`
  24. property of ``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
  25. :prop_tgt:`INTERFACE_COMPILE_OPTIONS` property of ``<target>``.
  26. The following arguments specify compile options. Repeated calls for the same
  27. ``<target>`` append items in the order called.
  28. .. versionadded:: 3.11
  29. Allow setting ``INTERFACE`` items on :ref:`IMPORTED targets <Imported Targets>`.
  30. .. |command_name| replace:: ``target_compile_options``
  31. .. include:: GENEX_NOTE.txt
  32. .. include:: OPTIONS_SHELL.txt
  33. See Also
  34. ^^^^^^^^
  35. * This command can be used to add any options. However, for adding
  36. preprocessor definitions and include directories it is recommended
  37. to use the more specific commands :command:`target_compile_definitions`
  38. and :command:`target_include_directories`.
  39. * For directory-wide settings, there is the command :command:`add_compile_options`.
  40. * For file-specific settings, there is the source file property :prop_sf:`COMPILE_OPTIONS`.
  41. * This command adds compile options for all languages in a target.
  42. Use the :genex:`COMPILE_LANGUAGE` generator expression to specify
  43. per-language compile options.
  44. * :command:`target_compile_features`
  45. * :command:`target_link_libraries`
  46. * :command:`target_link_directories`
  47. * :command:`target_link_options`
  48. * :command:`target_precompile_headers`
  49. * :command:`target_sources`
  50. * :variable:`CMAKE_<LANG>_FLAGS` and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`
  51. add language-wide flags passed to all invocations of the compiler.
  52. This includes invocations that drive compiling and those that drive linking.