What's up with the thousands of warnings in standard headers in MSVC -Wall?

后端 未结 6 561
既然无缘
既然无缘 2020-11-27 16:22

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 standar

相关标签:
6条回答
  • 2020-11-27 16:46

    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.

    0 讨论(0)
  • 2020-11-27 16:47

    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).

    0 讨论(0)
  • 2020-11-27 16:47

    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.

    0 讨论(0)
  • 2020-11-27 16:52

    For MSVC use /W4.

    0 讨论(0)
  • 2020-11-27 17:00

    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.

    0 讨论(0)
  • 2020-11-27 17:04

    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

    0 讨论(0)
提交回复
热议问题