13

I would like to have my warnings set to the highest level using Microsoft Visual C++ compiler. Similar to using -pedantic on gcc. What compiler switches do you use to have the most warnings enabled?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
KPexEA
  • 16,560
  • 16
  • 61
  • 78

6 Answers6

12

The highest warning level on Visual C++ is /Wall. The warning level can also be set numerically with /W0, /W1, ... /W4 to generate increasing levels of warnings.

The compiler will also check for 64 bit portability issues with /Wp64.

And you can tell it to treat warnings as errors with /WX

Visual C++ doesn't seem to have an real equivalent to -pedantic - which is asking gcc to report all warnings required to be reported by the ISO C and C++ standards.

billmcc
  • 711
  • 4
  • 11
5

AS billmcc64 mentioned, gcc's -pedantic causes gcc to warn if you use any non-standard extensions. I don't think you can get MSVC to warn for a similar situation, but you can cause it to generate an error for these situations using the /Za option.

Community
  • 1
  • 1
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
4

Note that /Wp64 is not recommended by Microsoft as opposed to compiling directly for the 64-bit platform if possible.

Also note that the Team Developer edition has a /analyze option, which adds semantic checking for common code errors. You can also look at getting PC-lint from gimpel for additional semantic checking.

Nick
  • 6,808
  • 1
  • 22
  • 34
  • 1
    The correct information is that where possible compiling directly for the 64-bit platform is preferable to using the /Wp64 option. – Andrew Grant Oct 21 '08 at 01:08
  • Yes; more specifically /Wp64 can give erroneous warnings and not give appropriate warnings compared to compiling directly for 64-bit. – Nick Oct 21 '08 at 02:13
  • The fun part is that compiling for 64-bit without /Wp64 will let you truncate pointers left and right without getting any warnings. – bk1e Oct 21 '08 at 04:42
  • 1
    The boring part is that compiling for 32-bit with /Wp64 will give you warnings even when you do what you should, e.g. use GetWindowLongPtr() and cast the result to a ptr... – Andreas Magnusson Oct 21 '08 at 16:35
3

if you want something like -pedantic, try a good lint like pc-lint (http://www.gimpel.com/html/products.htm).

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
3

Checkout the new /permissive- option which is supported in Visual Studio 2017 and later. The /permissive- option is compatible with almost all of the header files from the latest Windows Kits, such as the Software Development Kit (SDK) or Windows Driver Kit (WDK), starting in the Windows Fall Creators SDK (10.0.16299.0). Older versions of the SDK may fail to compile under /permissive- for various source code conformance reasons.

There is also the /Za option for disabling all the language extensions, but this also causes compilation errors when header files from the Windows Kits are used (such as winnt.h and winioctl.h). So, this option is not really viable.

See: https://learn.microsoft.com/en-us/cpp/build/reference/permissive-standards-conformance

boqpoq
  • 31
  • 1
1

/W4 /Wall should do the trick.

Menkboy
  • 1,595
  • 10
  • 12