How can I enable compiler warnings in Visual Studio 2019? - c++

The reason that I ask this question is this link below:
Why can this function return a C++ int reference?
It seems that the compiler is bad at reporting mistakes such as: return a value from a function.
So I want to activate them in Visual Studio 2019, but it did not work after I set it (restart IDE) like below:

I suggest you could try to use the following method to enable warnings that are off by default:
1,#pragma warning(default : warning_number )
The specified warning (warning_number) is enabled at its default level. Documentation for the warning contains the default level of the warning.
2,#pragma warning( warning_level : warning_number )
The specified warning (warning_number) is enabled at the specified level (warning_level).
3,/Wall
/Wall enables all warnings that are off by default. If you use this option, you can turn off individual warnings by using the /wd option.
4,/wLnnnn
This option enables warning nnnn at level L.
For more details anbout warning level, I suggest you could refer to the link:https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=vs-2019

Related

[[deprecated]] results in error instead of warning in Visual Studio

According to cppreference [[deprecated("message string")]] we should be able to use the symbol but in VS results in error instead.
For example I wan't to issue a warning for ANSI methods in UNICODE builds and vice versa:
#ifdef UNICODE
[[deprecated("This method does not work well in UNICODE builds")]]
#endif // UNICODE
void f() {}
compiler doesn't let me compile, but standard says the attribute should allow usage but issue a warning message.
How to resolve this?, btw. my project is set to maximum conformance with the standard.
What ever the reason for VS going against the standard, is there a better way to issue a warning for above case?
Very much late to the party, but this cost me a couple of hours this morning.
By default, Visual Studio and the sdl (Security Development Lifecycle) compile flag treat
[[deprecated]] as an error. Whether or not you agree with this or not, that's how they do it.
To fix this go to Configuration Properties -> C/C++ -> Command Line and add /sdl /w34996
The /wX part represents the severity of the warning, and the rest is the error you want to report as a warning.
I hope this saves people some time.
I was able to get the compiler to treat [[deprecated]] as a warning by adding
/w34996
to the compiler options, as suggested in the responses in following link
https://developercommunity.visualstudio.com/content/problem/786502/cant-treat-deprecated-warning-as-warning-with-wx.html
is there a better way to issue a warning for above case?
There's no other way of warning about usage of a function than deprecation attribute in standard C++ at least that I know of.
Msvc has other alternatives such as #pragma deprecated(f), but those are not better.
How to resolve this?
Assuming you haven't configured your compiler to treat warnings as errors, you could proceed with writing a bug report to the maintainers.
I haven't tested it with Visual Studio, but in my case the following helped and probably is more generic:
On that error usually warnings are treated as errors with the compiler flag -Werror, that is automatically set in Visual Studio. That is the reason way a class or function marked with [[deprecated]] results in an error. To avoid explicitly that error and output it just as a warning, the compiler flag -Werror -Wno-error=deprecated-declarations can be set. In CMake it would look like:
add_compile_options(-Werror -Wno-error=deprecated-declarations)

Warnings not being generated in VS2017

It seems to me that VS2017 fails to catch some fairly obvious compiler warnings which older versions do not. This seems so fundamental to me that I'm guessing the problem has to be something I'm missing (e.g. some new default compiler setting, perhaps?). Has anyone else seen this?
To test this, I create a simple console application in both 2013 and 2017. The only change I made the to the project settings was to set the compiler warning level to 4 and to report warnings as errors. Below is the entirety of the source
In VS2013 this fails. In 2017, it builds just fine...
// TestWarning4127.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
int main()
{
if (true) // *** SHOULD generate warning 4127
std::cout << "Warning";
return 0;
}
Am I missing something obvious here...?
When used inside an if or while conditions the trivial constants such as 1 or true do not generate warnings in VS 2017 as described in the Compiler Warning (level4) official documentation. Excerpt from the official documentation:
The controlling expression of an if statement or while loop evaluates
to a constant. Because of their common idiomatic usage, trivial
constants such as 1 or true do not trigger the warning, unless they
are the result of an operation in an expression.
That being said it's not a warning in VS 2013 with the default warning levels of W3 either. Only with the elevated W4 settings does the VS 2013 report a warning. It was removed in VS 2017 altogether.
For comparison, the GCC does not generate the warning either:
Live example on Coliru.

Can I treat a specific warning as an error?

The following is a simplified version of a pattern I sometimes see in my students' code:
bool foobar(int a, int b)
{
if (a < b) return true;
}
The real code is more complicated, of course. Visual Studio reports a warning C4715 (not all control paths return a value), and I would like to treat all warnings C4715 as errors. Is that possible?
This should do the trick: #pragma warning (error: 4715).
Or the /we4715 command line option (see /w, /W0, /W1, /W2, /W3, /W4, /w1, /w2, /w3, /w4, /Wall, /wd, /we, /wo, /Wv, /WX (Warning Level) (courtesy of Tom Sigerdas)).
/we4715 works for me.
In Visual Studio 2013 anyway, it is in the UI under Project Settings -> Configuration Properties -> C/C++ -> *Advanced *-> Treat Specific Warnings as Errors. Add "4715".
Docs: http://msdn.microsoft.com/en-us/library/thxezb7y.aspx
(Please note that this page lists the wrong UI property for VS2013.)
I added the following to the (VB)project file and it worked:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036,41997</WarningsAsErrors>
</PropertyGroup>
Set the compiler warning level to level 4 (in Visual Studio) and it will treat all warnings as errors. It is good practice to have your students compile their code with no warnings and no errors anyway :)
Also, turn on the /WX compiler option.

Enable a single warning in Visual Studio

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.
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.
#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
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
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.

How can I inhibit warning 4200 in Visual Studio 2005?

I can inhibit many warnings in Visual Studio 2005 SP1 in the C/C++ Advanced property page, which causes the IDE to use the /wd switch on the command line which invokes the compiler. However, when I try to inhibit warning 4200 (nonstandard extension used : zero-sized array in struct/union), it still appears when I compile. (Of course it's justified; I'm just not in a position to fix the code, nor would it be worth bothering, because it's generated, and the warning is entirely benign under the circumstances.) Does anybody happen to know if this is a bug in the compiler? Or might there be something I can do about it?
To completely disable the warning in the file you can add the following to the top of the file
#pragma warning(disable:2400)
If you want some more flexibility other than a blanket disable for the file, the following page lists several other more fine grained options.
http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
It's unclear based on your information as to whether or not it's a bug in the compiler or a configuration issue. I would lean towards a configuration issue, specifically conflicting compiler options that is making it difficult to suppress the warning.
EDIT
OP mentioned they can't control the generated code so they can't directly include the pragma. If that's the case then try this trick. Say the file name is Generated.cpp. No longer include Generated.cpp as one of the files to compile. Instead create a new file called Example.cpp with the following contents
#pragma warning(disable:2400)
#include "Generated.cpp"
Now you'll get the text of Generated.cpp with the disabled warning by only compiling Example.cpp.
You mean like with pragma?
#pragma warning( disable : 2400 )