Enable a single warning in Visual Studio

2019-01-11 22:40发布

Is there a compiler switch to enable a single warning in 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.

Even Microsoft pages called How to: Enable or Disable Compiler Warnings still only mention disabling.

4条回答
Explosion°爆炸
2楼-- · 2019-01-11 22:57

If you want to turn it on (or off) in the project setting, you have to go to:

Configuration Properties -> C/C++ -> Command Line and then under Additional Options you can enter:

/w3#### to set your warning to level 3, and thus enable it; or you can enter /wd#### to disable a warning.

查看更多
干净又极端
3楼-- · 2019-01-11 23:11

Use:

#pragma warning(default:4265)

and compile with at least /W3.

Here's an explicit example from Microsoft:

http://msdn.microsoft.com/en-us/library/wzxffy8c(v=VS.90).aspx

查看更多
Melony?
4楼-- · 2019-01-11 23:13

To make the comment of Matthäus Brandl regarding #pragma warning more visible:

If you're compiling with a warning level lower than 3, you have to use this syntax:

#pragma warning (<warning level>: 4265)

Only if you compile with level 3 or higher you can do

#pragma warning (default: 4265)

because for warning 4265, default means level 3 (see MSDN).

The documentation for #pragma warning reads:

warning-specifier Meaning

1, 2, 3, 4 Apply the given level to the specified warning(s). This also turns on a specified warning that is off by default.

default Reset 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.

查看更多
虎瘦雄心在
5楼-- · 2019-01-11 23:17
#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

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
查看更多
登录 后发表回答