10

I'm using MSVC with a CMaked project. As a result, I've enabled many of the flags on MSVC which were enabled for gcc and clang. However, the /Wall warning level is giving me some pain; it warns me about all kinds of things in included headers, like stdio.h and boost headers. Is there a way to stop MSVC from warning me about things in headers? I like my warning levels, but I only want them enabled for me.

bfops
  • 5,348
  • 5
  • 36
  • 48
  • Possible duplicate of [What's up with the thousands of warnings in standard headers in MSVC -Wall?](http://stackoverflow.com/questions/4001736/whats-up-with-the-thousands-of-warnings-in-standard-headers-in-msvc-wall) –  Jul 23 '16 at 14:07
  • vote for https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/14717934-add-a-cl-exe-option-for-system-headers-like-gcc-s – Trass3r Jul 20 '17 at 11:42

5 Answers5

10

/Wall is very pedantic. /W4 is probably all you really need. To answer your question, you can disable specific warnings around your headers with:

 #pragma warning(disable:xxxx)
 #include <yourheader.h>
 #pragma warning(default:xxxx)

Or change the warning level with:

 #pragma warning(push,3)
 #include <yourheader.h>
 #pragma warning(pop)

See the MSDN documentation: http://msdn.microsoft.com/en-us/library/2c8f766e.aspx

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • So there is no way to tell MSVC to not warn in headers over which I have no control? (i.e. all headers included in angle brackets, not double quotes) – bfops Nov 27 '10 at 15:54
  • 1
    No, there isn't. Additionally, for some templates you need to disable the warnings not only around the headers, but also where you instantiate them (but maybe this was only a problem with older version... I'm not so sure here, sry). Take extra care when using precompiled headers: you need to disable everything there, too. – gimpf Nov 27 '10 at 15:59
4

You can use a different warning level for headers "external" to your project :

/external:anglebrackets /external:W3

See /external (External headers diagnostics) for details.

diapir
  • 2,872
  • 1
  • 19
  • 26
0

You can disable specific warnings using the /wdXXXX flag where XXXX is the number of the warnings you wish to ignore. No need to modify the code.

Mikael Öhman
  • 2,294
  • 15
  • 21
0

For a long time MSVC lacked the functionality to properly deal with this situation unlike GCC/Clang.

Now there is a solution for this.

The /external compiler options are available starting in Visual Studio 2017 version 15.6. In versions of Visual Studio before Visual Studio 2019 version 16.10, the /external options require you also set the /experimental:external compiler option.

Essentially this is MSVC's version of -isystem which has long been a feature of GCC / Clang.

Still though. Given how new this feature is, library writers should still make sure their public facing header files compile as cleanly as possible on MSVC.

Many users may not be able to update their VS version, or won't be aware of this new functionality.

Links:

Original blog post: https://devblogs.microsoft.com/cppblog/customized-warning-levels-and-code-analysis-for-external-headers/

Official documentation: https://learn.microsoft.com/en-us/cpp/build/reference/external-external-headers-diagnostics?view=msvc-170

jpr42
  • 718
  • 3
  • 14
-1

Mark Tolonen has already point out /W4.

If that still produces warnings, e.g. you're using an older MSVC version like 7.1, or you're using some 3rd party library that still produces warnings about perfectly good code, and you're aiming for clean compiles, then see my msvc silly-warning suppression header.

It's been through a few rounds of community review, in the comp.lang.c++ Usenet group, but it may/will need updating as Microsoft adds even more silly-warnings in new compiler versions… ;-)

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • I especially like the new warning in VC++10 "warning: elements of the array will be default initialized" as if there was anything wrong with that :/ – Matthieu M. Nov 27 '10 at 18:28