# Compiler options for warnings

**URL:** https://discourse.cmake.org/t/compiler-options-for-warnings/9936
**Category:** Usage
**Created:** [January 25, 2024, 7:17pm UTC](https://discourse.cmake.org/t/compiler-options-for-warnings/9936 "2024-01-25T19:17:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Volker\_Dirr](https://discourse.cmake.org/user_avatar/discourse.cmake.org/volker_dirr/32/4231_2.png) [@Volker\_Dirr](https://discourse.cmake.org/u/Volker_Dirr)
#### Post date: [January 25, 2024, 7:17pm UTC](https://discourse.cmake.org/t/compiler-options-for-warnings/9936/1 "2024-01-25T19:17:05Z")

</div>

Hallo,

I have a question about setting the compiler options for warnings.  
I read in the documentation this:

```auto
if (MSVC)
    # warning level 4
    add_compile_options(/W4)
else()
    # additional warnings
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

```

see [https://cmake.org/cmake/help/latest/command/add\_compile\_options.html](https://cmake.org/cmake/help/latest/command/add_compile_options.html)

I wonder why the documentation didn’t use /Wall for MSVC.  
I mean it added even -Wpedantic for other compilers, but only /W4 for MSVC, even /Wall do more tests.  
see:

> **[/w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv,...](https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=msvc-170)**
>
> Reference for the Microsoft C/C++ compiler options: /w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv, and /WX.

So what is the reason for this? Maybe because of older MSVC versions?

Thank you

---

<div class="post-metadata">

### Author: ![dnglaze](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/74df32/32.png) [@dnglaze](https://discourse.cmake.org/u/dnglaze)
#### Post date: [January 25, 2024, 7:41pm UTC](https://discourse.cmake.org/t/compiler-options-for-warnings/9936/2 "2024-01-25T19:41:44Z")

</div>

`/Wall` for MSVC can produce a lot of warnings that I’m guessing most folks don’t find particularly useful. Almost to the point where it’s overly chatty and loses its utility.

I’ve gone to looking at [this CMake template](https://github.com/cpp-best-practices/cmake_template/blob/main/cmake/CompilerWarnings.cmake) to serve as an example of what’s considered the current best practices for warnings for each of the compilers. You’ll notice it also doesn’t use `/Wall` on MSVC. The CMake documentation itself is likely just trying to show an example rather than what’s the current best practice.

As an aside, it’s preferred to use `target_compile_options` and assign the options to the target itself rather than using the directory functions `add_compile_options`. You’ll notice in the template I linked, it’s using an interface target to manage the compile options being applied. This leaves it to project targets to link with this interface target to apply the compile options.

---

<div class="post-metadata">

### Author: ![Volker\_Dirr](https://discourse.cmake.org/user_avatar/discourse.cmake.org/volker_dirr/32/4231_2.png) [@Volker\_Dirr](https://discourse.cmake.org/u/Volker_Dirr)
#### Post date: [January 25, 2024, 7:51pm UTC](https://discourse.cmake.org/t/compiler-options-for-warnings/9936/3 "2024-01-25T19:51:23Z")

</div>

Thank you for your answer. And thank you for the link! I need more time to read and understand everything, but it already looks like a very good answer.
