MSVC equivalent of gcc/clang's -Wall?

2019-07-04 17:15发布

问题:

I often build C (and C++) code, using GCC (or clang) with the -Wall flag turned on. Now I happen to need to make sure a small C project, which builds fine on Linux with this flag, also builds on Windows with MSVC.

However, if I run MSVC with -Wall, I get many warnings which I find rather spurious, such as:

  • warning C4255: 'some_func': no function prototype given: converting '()' to '(void)'
  • `warning C4820: 'some_struct': '4' bytes padding added after data member 'some_member'

and so on. Now, I realize I can suppress individual warnings with #pragma warning(disable:1234); but still: What's considered a common, reasonable combination of compiler warning switches for C code with MS Visual C++, which is roughly equivalent to gcc/clang's -Wall switch?

Edit: If I were asking about -Wall -Wextra, that would be this existing question.

Note: I'm using MSVC 2015 in case it matters - but not the IDE, just the compiler.

回答1:

Be advised that MSVC is woefully non-compliant for standard C code. As you can see, it generates quite a few useless warnings, and it rarely enforces C99 or C11 provisions.

The typical approach is to define /Wall and to suppress specific warnings in the project's property sheet, so that you don't have to sprinkle #pragma directives all over your code. Of course, if you're building from the command line, then you're stuck with using #pragmas.

Also, it's very often useful to define CRT_SECURE_NO_WARNINGS to eliminate all of the warnings that favor using MS-approved versions of most of the string functions. This naturally assumes that you're not actually using those versions.

About the C4255 warning -- this is actually required according to the standard, since empty parameter lists are no longer supported. The point here is to make sure that while you're suppressing all of the fluff, you don't actually suppress something useful.