visual-studio 在 Visual Studio 中启用单个警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4151908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable a single warning in Visual Studio
提问by 0xC0DEFACE
Is there a compiler switch to enable a single warning in Visual Studio?
是否有编译器开关可以在 Visual Studio 中启用单个警告?
The reason I ask is I want to enable warning C4265 which is off by default. My searching has only turned up ways to turn warnings off.
我问的原因是我想启用默认情况下关闭的警告 C4265。我的搜索只找到了关闭警告的方法。
Even Microsoft pages called How to: Enable or Disable Compiler Warningsstill only mention disabling.
即使是名为How to: Enable or Disable Compiler Warnings 的Microsoft 页面仍然只提到禁用。
采纳答案by Martin Ba
If you want to turn it on (or off) in the project setting, you have to go to:
如果你想在项目设置中开启(或关闭)它,你必须去:
Configuration Properties -> C/C++ -> Command Lineand then under Additional Optionsyou can enter:
Configuration Properties -> C/C++ -> Command Line然后在附加选项下,您可以输入:
/w3####to set your warning to level 3, and thus enable it; or you can enter /wd####to disable a warning.
/w3####将您的警告设置为 3 级,从而启用它;或者您可以输入/wd####以禁用警告。
Current (2015,2017,2019,...) Visual Studio Versions also have a dedicated setting to disable warnings under:
当前 (2015,2017,2019,...) Visual Studio 版本也有一个专用设置来禁用以下警告:
Configuration Properties -> C/C++ -> Advanced : Disable Specific Warnings... is equivalent to /wd####.
Configuration Properties -> C/C++ -> Advanced : Disable Specific Warnings... 相当于/wd####.
Also useful in recent versions: C/C++ -> All Optionsand then filter for e.g. "warn".
在最近的版本中也很有用:C/C++ -> All Options然后过滤例如“警告”。
It would appear that enabling á la /w3####is not yet exposed explicitly.
似乎/w3####还没有明确公开启用 á la 。
回答by Benjamin Lindley
#pragma warning(default:4265)
It might seem like that would set the warning to it's default setting(which would be disabled), but that's not the case. It turns it on.
这似乎会将警告设置为其默认设置(将被禁用),但事实并非如此。它打开它。
http://msdn.microsoft.com/en-us/library/2c8f766e%28VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/2c8f766e%28VS.80%29.aspx
You can also do this:
你也可以这样做:
#pragma warning(X:4265)
// where X is the warning level(1,2,3 or 4) that you want this warning to be generated at
回答by Andreas Haferburg
To make the comment of Matth?us Brandl regarding #pragma warningmore visible:
为了让马特斯·布兰德尔的评论#pragma warning更加明显:
If you're compiling with a warning level lower than 3, you have to use this syntax:
如果编译时警告级别低于 3,则必须使用以下语法:
#pragma warning (<warning level>: 4265)
Only if you compile with level 3 or higher you can do
只有当你用 3 级或更高级别编译时,你才能做到
#pragma warning (default: 4265)
because for warning 4265, defaultmeans level 3 (see MSDN).
因为对于警告 4265,default意味着级别 3(请参阅MSDN)。
The documentation for #pragma warningreads:
warning-specifierMeaning
1, 2, 3, 4Apply the given level to the specified warning(s). This also turns on a specified warning that is off by default.
defaultReset warning behavior to its default value. This also turns on a specified warning that is off by default. The warning will be generated at its default, documented, level.
warning-specifier意义
1, 2, 3, 4将给定的级别应用于指定的警告。这也会打开默认情况下关闭的指定警告。
default将警告行为重置为其默认值。这也会打开默认情况下关闭的指定警告。警告将在其默认的、记录的、级别生成。
回答by Mark Tolonen
Use:
利用:
#pragma warning(default:4265)
and compile with at least /W3.
并至少编译/W3。
Here's an explicit example from Microsoft:
这是来自微软的一个明确的例子:
http://msdn.microsoft.com/en-us/library/wzxffy8c(v=VS.90).aspx
http://msdn.microsoft.com/en-us/library/wzxffy8c(v=VS.90).aspx

