73

Some people seem to advise you use -Wall, but when I did it on a small test project which just has a main.cpp with some includes, I get 5800 warnings most of them in standard headers or in windows headers.

Is that intended behaviour? How do I go about making my compilation warning free?

Here are just a few for some reading fun:

1>c:\program files\microsoft visual studio 10.0\vc\include\stdint.h(105): warning C4668: '_INTPTR' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(109): warning C4820: '_wfinddata64i32_t' : '4' bytes padding added after data member '_wfinddata64i32_t::attrib'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(114): warning C4820: '_wfinddata64i32_t' : '4' bytes padding added after data member '_wfinddata64i32_t::name'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(118): warning C4820: '_wfinddata64_t' : '4' bytes padding added after data member '_wfinddata64_t::attrib'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(488): warning C4820: '_stat32' : '2' bytes padding added after data member '_stat32::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(504): warning C4820: 'stat' : '2' bytes padding added after data member 'stat::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(520): warning C4820: '_stat32i64' : '2' bytes padding added after data member '_stat32i64::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(521): warning C4820: '_stat32i64' : '4' bytes padding added after data member '_stat32i64::st_rdev'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(525): warning C4820: '_stat32i64' : '4' bytes padding added after data member '_stat32i64::st_ctime'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(534): warning C4820: '_stat64i32' : '2' bytes padding added after data member '_stat64i32::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(548): warning C4820: '_stat64' : '2' bytes padding added after data member '_stat64::st_gid'
1>c:\program files\microsoft visual studio 10.0\vc\include\wchar.h(549): warning C4820: '_stat64' : '4' bytes padding added after data member '_stat64::st_rdev'
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdbg.h(1078): warning C4986: 'operator new[]': exception specification does not match previous declaration
1>          c:\program files\microsoft visual studio 10.0\vc\include\new(79) : see declaration of 'operator new[]'
1>c:\program files\microsoft visual studio 10.0\vc\include\crtdbg.h(1095): warning C4986: 'operator delete[]': exception specification does not match previous declaration
1>          c:\program files\microsoft visual studio 10.0\vc\include\new(77) : see declaration of 'operator delete[]'
1>c:\program files\microsoft visual studio 10.0\vc\include\typeinfo(76): warning C4820: 'type_info' : '3' bytes padding added after data member 'type_info::_M_d_name'
1>c:\program files\microsoft sdks\windows\v7.0a\include\basetsd.h(114): warning C4668: '__midl' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8154): warning C4820: '_SECURITY_QUALITY_OF_SERVICE' : '2' bytes padding added after data member '_SECURITY_QUALITY_OF_SERVICE::EffectiveOnly'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8165): warning C4820: '_SE_IMPERSONATION_STATE' : '2' bytes padding added after data member '_SE_IMPERSONATION_STATE::EffectiveOnly'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8334): warning C4820: '_QUOTA_LIMITS' : '4' bytes padding added after data member '_QUOTA_LIMITS::PagefileLimit'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8357): warning C4820: '_QUOTA_LIMITS_EX' : '4' bytes padding added after data member '_QUOTA_LIMITS_EX::PagefileLimit'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8405): warning C4820: '_JOBOBJECT_BASIC_LIMIT_INFORMATION' : '4' bytes padding added after data member '_JOBOBJECT_BASIC_LIMIT_INFORMATION::SchedulingClass'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(8984): warning C4820: '_FILE_NOTIFY_INFORMATION' : '2' bytes padding added after data member '_FILE_NOTIFY_INFORMATION::FileName'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(9012): warning C4820: '_REPARSE_GUID_DATA_BUFFER' : '3' bytes padding added after data member '_REPARSE_GUID_DATA_BUFFER::GenericReparseBuffer'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10131): warning C4820: '<unnamed-tag>' : '3' bytes padding added after data member '<unnamed-tag>::Data'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10241): warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member '<unnamed-tag>::DecreaseTime'
1>c:\program files\microsoft sdks\windows\v7.0a\include\winnt.h(10262): warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member '<unnamed-tag>::TimerInterval' 
1>c:\program files\microsoft sdks\windows\v7.0a\include\wincrypt.h(1440): warning C4668: 'NTDDI_WINLH' is not defined as a preprocessor macro, replacing with '0' for '#if/
  • There's a reason that those warnings are disabled by default. Don't use -Wall – Benjamin Lindley Oct 23 '10 at 00:02
  • 1
    GCC has a way to suppress warnings from system and library headers (`-isystem` instead of `-I` and `#pragma GCC system_header`). Is the same not true of VC or does `/Wall` disable even that? – greyfade Oct 23 '10 at 00:35
  • 1
    possible duplicate of [MSVC - stop warnings in headers](http://stackoverflow.com/questions/4292352/msvc-stop-warnings-in-headers) – Cody Gray - on strike Jul 18 '11 at 04:31
  • 1
    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

6 Answers6

46

The Visual C++ /Wall enables all of the warnings that are disabled by default at /W4. As you've found out, there is a good reason why a lot of those warnings are disabled by default (thanks, compiler, for telling me you've added padding; I really appreciate it!). It's probably best just to use /W4 on Visual C++.

Intel C++ is like this too (I don't know about other compilers that utilize the EDG frontend). If you set it at /W5, it spews out tons of informational messages. My personal favorite is that it warns you if the storage class specifier isn't at the beginning of a declaration (so, const static int is no go, but static const int is fine).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 35
    This is actually quite depressing that the VS libraries don't allow use of -Wall. There are also very good reasons why all the warnings should be emitted. It is simply poor coding that the standard headers are producing such warnings -- or the compiler is emitting nonsensical warnings (which VS has several of). – edA-qa mort-ora-y Oct 23 '10 at 07:34
  • 9
    @edA: I don't know what's so "depressing" that the use of `/Wall` causes lots of warnings: as I explained in the answer, `/Wall` enables many warnings that, while potentially useful sometimes, are not useful most of the time. `/Wall` in Visual C++ does not mean the same thing as `-Wall` on g++ (really, g++ "has `/Wall` wrong," since it doesn't actually enable _all_ warnings). In any case, in Visual C++, all of the commonly important and useful warnings are enabled by `/W4`. – James McNellis Sep 12 '11 at 13:31
  • 44
    If you can't compile a program using the standard library and not get warnings it pretty much makes the option useless. You won't be able to sort through library warnings and warnings in your code. I'm only interested in things in my code, not the standard library. – edA-qa mort-ora-y Sep 12 '11 at 14:35
  • 2
    @edA-qamort-ora-y The issue is that a lot of the "warnings" should actually be notes, instead. Case in point, C4820 ("Warning: I added padding.") or C4514 ("Warning: I optimised an inline function you never called out."). Some are useful to know, but not really something you can change (such as C4710 ("Warning: `inline` function not actually inlined.") for `std::swprintf()`, [because it has a variable argument list](https://msdn.microsoft.com/en-us/library/a98sb923.aspx)). – Justin Time - Reinstate Monica Nov 20 '16 at 20:25
  • Some are _really_ useful, but should still be notes. Case in point, C4514 (again), when the unreferenced function is, say, something like `std::bad_alloc::bad_alloc`; you can check the C4514 warnings for a quick-and-dirty list of exceptions that won't be thrown, among other things. – Justin Time - Reinstate Monica Nov 20 '16 at 20:29
  • And some are valid warnings, but effectively meaningless due to compiler design. Case in point, it might emit C4986 for, say, `operator new[]`'s exception specification in one implementation header not matching the specification in another, when [MSVC treats every exception specifier other than `throw()`, including _no specifier_, as `throw(...)`](https://msdn.microsoft.com/en-ca/library/wfa0edys.aspx). – Justin Time - Reinstate Monica Nov 20 '16 at 20:30
28

To disable warnings from system headers over which you have no control just use this construct:

#pragma warning(push, 0)       
//Some includes with unfixable warnings
#pragma warning(pop)

or more selectively for specific warnings:

#pragma warning( push )
#pragma warning( disable : 4081)
#pragma warning( disable : 4706 )
// system header includes 
#pragma warning( pop )

This answer was purloined from another Stack Overflow thread: (https://stackoverflow.com/questions/2541984/how-to-suppress-warnings-in-external-headers-in-visual-c).

I fully agree with the comments made by "edA-qa mort-ora-y". I want to see all warnings in my code, including important stuff like C4265 (DTOR not virtual). Although C4265 is at warning level 3, Microsoft in their wisdom have switched it off by default and you need /Wall to get it. See this page for more information about which warnings are hidden:

http://msdn.microsoft.com/en-GB/library/23k5d385(v=vs.80).aspx

To see these and to suppress the noise from the external headers, this page gives great advice, and I think fully answers the original question which started this thread:

http://blogs.msdn.com/b/vcblog/archive/2010/12/14/off-by-default-compiler-warnings-in-visual-c.aspx

Basically it advises to create a 'global' include file with the appropriate #pragmas to suppress the warnings you don't care about (maybe C4820 the padding one), to guard against external headers in the manner described above, then the compile with /Wall. That's a piece of work, but worth it. Under GCC it would just be a question of using -isystem. Microsoft development: take note! VS is a smart product but it's really dumb sometimes with the simple stuff.

Community
  • 1
  • 1
woodspiral
  • 425
  • 4
  • 3
  • 1
    You don't have to use `/Wall`. You can enable that specific one with `/w34265`. On the other hand you can use `/Wall` and then disable specific ones like the padding warning with `/wd4820`. I agree though that `-isystem` would be an useful feature. – cremno Feb 27 '15 at 12:29
9

I know that this is late in the game but I believe I have a way to use /Wall for your own files but not have to see the noise from the Microsoft or other "external" headers. This assumes you are using precompiled headers via stdafx.h.

  1. For the project, set the warning level to /Wall (maximum warnings)
  2. For the file stdafx.cpp, set the warning level to /W4 (lots of warnings enabled but MS headers pass silently)
  3. For the project, under Disable Specific Warnings, add 4652

The first two seem obvious. But when stdafx.h is included in your own files, the warning levels do not match and warning C4652 is issued. Which defeats the whole exercise. But now that message is also suppressed.

It is kinda tedious to do this for each new project but not as bad as lots of individual #pragma warning() suppressions.

Harold Bamford
  • 1,589
  • 1
  • 14
  • 26
6

For MSVC use /W4.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
2

in MSVC 2010

Options

Coniguration Properties

C/C++

Advanced

Disable Specific Warnings

set a value like 4820;4996;4514;4710 to explicity disable the warnings that you consider unimportant. At that point you can -WALL without concern

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
  • 1
    To be fair, C4514 can be pretty useful sometimes, it just isn't most of the time. "C4514: `std::bad_alloc::bad_alloc` optimised out" (paraphrased) is good to know, for example (guarantees that no `bad_alloc` exceptions will be thrown), but something like "C4514: 'std::_Maklocbyte' optimised out" or "C4514: 'std::to_wstring' optimised out" (paraphrased) would generally be less so. – Justin Time - Reinstate Monica Nov 20 '16 at 20:37
1

I have the same initial problem when compiling various software stacks with VC6 and Microsoft Platform SDK (in BaseTsd.h for example).

What I (we) want to do is to control the compiler warning level for our code - we want to be able to play with the /W flag. In practice, as already pointed out, /W4 enables all generally useful warnings (and also some spurious...).

As the problem comes from the MSFT header files, I modify in a clean way the Microsoft-supplied header files. There are not so many changes to implement.

If compiler complains with warning C4305, in the source file I insert:

#pragma warning( disable : 4305)

before the offending line, followed by:

#pragma warning( default : 4305)

after the offending line. No side effect. Microsoft could have done it this way, probably.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74