add_compile_options.rst 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. Arguments
  10. ^^^^^^^^^
  11. .. |command_name| replace:: ``add_compile_options``
  12. .. include:: GENEX_NOTE.txt
  13. .. include:: OPTIONS_SHELL.txt
  14. Example
  15. ^^^^^^^
  16. Since different compilers support different options, a typical use of
  17. this command is in a compiler-specific conditional clause:
  18. .. code-block:: cmake
  19. if (MSVC)
  20. # warning level 4
  21. add_compile_options(/W4)
  22. else()
  23. # additional warnings
  24. add_compile_options(-Wall -Wextra -Wpedantic)
  25. endif()
  26. To set per-language options, use the :genex:`$<COMPILE_LANGUAGE>`
  27. or :genex:`$<COMPILE_LANGUAGE:languages>` generator expressions.
  28. See Also
  29. ^^^^^^^^
  30. * This command can be used to add any options. However, for
  31. adding preprocessor definitions and include directories it is recommended
  32. to use the more specific commands :command:`add_compile_definitions`
  33. and :command:`include_directories`.
  34. * The command :command:`target_compile_options` adds target-specific options.
  35. * The source file property :prop_sf:`COMPILE_OPTIONS` adds options to one
  36. source file.