27

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
0xC0DEFACE
  • 7,938
  • 7
  • 31
  • 34

4 Answers4

26
#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
Benjamin Lindley
  • 95,516
  • 8
  • 172
  • 256
  • 2
    +1. Yes, the word *default* means *default warning level*, not *default state*. Thanks for pointing this out, I didn't find it myself. – sharptooth Nov 11 '10 at 06:57
  • i was looking for a compiler flag to do this rather than a code change, as then the warning isn't defined project wide, however it seems this is the only way to achieve the result :( – 0xC0DEFACE Nov 18 '10 at 00:56
  • 4
    This will not work if the _default warning level_ of the selected warning is less (in terms of severity) than the level set up in the project properties. **Example:** Warning level is set to 3 (/W3), but warning [C4245](http://msdn.microsoft.com/en-us/library/e9s7thk1.aspx) has a default level of 4. Using `#pragma warning(default:4245)` will not enable it, but `#pragma warning(3:4245)` does. (this just bit me) – Brandlingo Sep 02 '14 at 11:56
25

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.


Current (2015,2017,2019,...) Visual Studio Versions also have a dedicated setting to disable warnings under:

Configuration Properties -> C/C++ -> Advanced : Disable Specific Warnings ... is equivalent to /wd####.

Also useful in recent versions: C/C++ -> All Options and then filter for e.g. "warn".

It would appear that enabling á la /w3#### is not yet exposed explicitly.

Martin Ba
  • 33,741
  • 27
  • 150
  • 304
  • The best answer! Thanks a lot! I haven't seen it mentioned anywhere on MSDN. – Violet Giraffe Mar 21 '14 at 12:01
  • Actually does not work with VStudio 2015. Added it to every single project of Synergy, but got still the build error "Please enable it". Use additional parameter **/w34005** for All Configurations. – Slesa Jun 21 '16 at 09:33
  • @Slesa - of course it works in VS2015. (I just tried it) What did you do? Post a new question. – Martin Ba Jun 21 '16 at 10:16
  • My warning I want to enable is 4706. For me it is also not working with VS2017 either with /w34706 nor /w44706. – jaba Jul 19 '18 at 20:21
  • @jaba I'm pretty sure that if you write up a proper question and try to include all necessary details, you'll find the problem. – Martin Ba Jul 20 '18 at 10:13
  • If you are using /w2, then to enable a specific warning it is necessary to use /w24706. This tells the compiler to treat warning 4706 as a level 2 warning. – Jeffrey Faust Apr 16 '20 at 16:17
1

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

Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208
1

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.

Community
  • 1
  • 1
Andreas Haferburg
  • 4,678
  • 1
  • 29
  • 52