add_compile_options.rst 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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:: GENEX_NOTE.txt
  16. .. include:: OPTIONS_SHELL.txt
  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>` generator expressions.
  31. See Also
  32. ^^^^^^^^
  33. * This command can be used to add any options. However, for
  34. adding preprocessor definitions and include directories it is recommended
  35. to use the more specific commands :command:`add_compile_definitions`
  36. and :command:`include_directories`.
  37. * The command :command:`target_compile_options` adds target-specific options.
  38. * This command adds compile options for all languages.
  39. Use the :genex:`COMPILE_LANGUAGE` generator expression to specify
  40. per-language compile options.
  41. * The source file property :prop_sf:`COMPILE_OPTIONS` adds options to one
  42. source file.
  43. * :command:`add_link_options` adds options for linking.
  44. * :variable:`CMAKE_<LANG>_FLAGS` and :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`
  45. add language-wide flags passed to all invocations of the compiler.
  46. This includes invocations that drive compiling and those that drive linking.