How do I set warning levels and warnings-as-errors with MSVC?

In MSVC (Microsoft Visual C++), you can set warning levels and treat warnings as errors using the following options:

To set the warning level, you can use the /W flag followed by a number representing the desired warning level. The levels are:

  • /W0 - No warnings
  • /W1 - Level 1 warnings
  • /W2 - Level 2 warnings
  • /W3 - Level 3 warnings (default)
  • /W4 - Level 4 warnings (most detailed)
  • /Wall - All warnings

To treat all warnings as errors, you can use the /WX option. This will make the compiler stop compilation when it encounters any warning.

Example

cl /W4 /WX your_file.cpp

keywords: MSVC warning levels warnings as errors C++ compiler /W flag