add_compile_options.rst 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. add_compile_options
  2. -------------------
  3. Add options to the compilation of source files.
  4. .. code-block:: cmake
  5. add_compile_options(<option> ...)
  6. Adds options to the :prop_dir:`COMPILE_OPTIONS` directory property.
  7. These options are used when compiling targets from the current
  8. directory and below.
  9. .. note::
  10. These options are not used when linking.
  11. See the :command:`add_link_options` command for that.
  12. Arguments
  13. ^^^^^^^^^
  14. .. |command_name| replace:: ``add_compile_options``
  15. .. include:: include/GENEX_NOTE.rst
  16. .. include:: include/OPTIONS_SHELL.rst
  17. Example
  18. ^^^^^^^
  19. Since different compilers support different options, a typical use of
  20. this command is in a compiler-specific conditional clause:
  21. .. code-block:: cmake
  22. if (MSVC)
  23. # warning level 4
  24. add_compile_options(/W4)
  25. else()
  26. # additional warnings
  27. add_compile_options(-Wall -Wextra -Wpedantic)
  28. endif()
  29. To set per-language options, use the :genex:`$<COMPILE_LANGUAGE>`
  30. or :genex:`$<COMPILE_LANGUAGE:languages> <COMPILE_LANGUAGE:languages>`
  31. generator expressions.
  32. See Also
  33. ^^^^^^^^
  34. * This command can be used to add any options. However, for
  35. adding preprocessor definitions and include directories it is recommended
  36. to use the more specific commands :command:`add_compile_definitions`
  37. and :command:`include_directories`.
  38. * The command :command:`target_compile_options` adds target-specific options.
  39. * This command adds compile options for all languages.
  40. Use the :genex:`COMPILE_LANGUAGE` generator expression to specify
  41. per-language compile options.
  42. * The source file property :prop_sf:`COMPILE_OPTIONS` adds options to one
  43. source file.
  44. * :command:`add_link_options` adds options for linking.
  45. * :variable:`CMAKE_<LANG>_FLAGS` and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`
  46. add language-wide flags passed to all invocations of the compiler.
  47. This includes invocations that drive compiling and those that drive linking.