MSVC相当于gcc / clang的-Wall? [英] MSVC equivalent of gcc/clang's -Wall?

查看:162
本文介绍了MSVC相当于gcc / clang的-Wall?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用GCC(或clang)并打开 -Wall 标志来构建C(和C ++)代码。现在我碰巧需要确保一个小的C项目,该项目可以在Linux上使用此标志很好地构建,也可以在Windows上使用MSVC进行构建。使用 -Wall ,我收到许多我觉得很虚假的警告,例如:




  • 警告C4255:'some_func':未提供函数原型:将'()'转换为'(void)'

  • `警告C4820: some_struct:在数据成员 some_member之后添加了 4个字节填充



,依此类推。现在,我意识到我可以使用 #pragma warning(disable:1234)取消单个警告了;但仍然:认为是C语言与MS Visual C ++的编译器警告开关的常见合理组合,大致等效于 gcc / clang -墙壁开关?



编辑:我问的是 -Wall -Wextra ,那就是 this现有问题



注意:如果有问题,我将使用MSVC 2015-但不是IDE,而是编译器。

解决方案

请注意,MSVC非常不符合标准C代码的要求。如您所见,它会生成很多无用的警告,并且很少执行C99或C11规定。



典型的方法是定义/ Wall并禁止显示特定警告在项目的属性表中,这样您就不必在整个代码中使用 #pragma 指令。当然,如果您是从命令行构建的,那么就不得不使用 #pragmas



此外,定义 CRT_SECURE_NO_WARNINGS 通常非常有用,以消除所有赞成使用MS批准的大多数字符串函数版本的警告。自然会以为您实际上并没有使用这些版本。



关于 C4255 警告-这实际上是由于不再支持空参数列表,因此根据标准是必需的。这里的目的是确保在抑制所有绒毛的同时,您实际上并未抑制有用的东西。


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.

解决方案

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.

这篇关于MSVC相当于gcc / clang的-Wall?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆