Enable a single warning in Visual Studio - c++

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.

Related

How can I enable compiler warnings in Visual Studio 2019?

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

CMake: How to treat every resource compiler warning as error and to suppress specific warnings?

To suppress specific warning in specific source file when using CMake to generate MSVC projects I'm using something like:
set_source_files_properties(
"ToursInfoMng.cpp"
PROPERTIES
COMPILE_FLAGS "/wd4503")
This doesn't work for resource compiler warnings. For example for warning:
warning RC4206: title string too long; truncated at 256
I tried to use:
set_source_files_properties(
"ResEs.rc"
PROPERTIES
COMPILE_FLAGS "/wd4206")
but this doesn't suppress the warning.
How to suppress resource compiler warning properly?
How to treat resource compiler warnings which are not suppressed as errors?
I'm using \WX for both compiler and linker warnings setting it to CMAKE_CXX_FLAGS and CMAKE_STATIC|SHARED|EXE_LINKER_FLAGS respectively, but I don't know how to do it for resource compiler warnings.
I don't believe either of these is possible. There are no documented general warning control options for the Windows Resource Compiler, just type rc /? to check.
You can change the Resource Compiler's flags using CMAKE_RC_FLAGS.
There is also a filter on COMPILE_FLAGS and COMPILE_OPTIONS that prevents them being used to pass any flags that are not defines or includes to the Resource Compiler. In principle, if you needed to, you could change the filter (CMAKE_RC_FLAG_REGEX) so as to be able to use these properties for more, but that would not help you in this situation.
#pragma warning(push)
#pragma warning(disable : 4101)
// Function Here : Declare
#pragma warning(pop)
Then If you want To suppress specific warnings for Visual C++
Following this ref : https://msdn.microsoft.com/en-us/library/jj715718.aspx
Press Ctrl+F > Type "To suppress specific warnings for Visual C++".
You can find how to suppress specific warnings.

How to enable warnings through pragma?

I'm trying to enable some warnings that detail potential security problems through a pragma. The warnings are those listed by Jon Sturgeon in “Off By Default” Compiler Warnings in Visual C++:
# pragma warning (enable: 4191 4242 4263 4264 4265 4266 4302 4826 4905 4906 4928)
I'm getting a warning under Visual Studio (2010 to be exact) for the above pragma:
ClCompile:
pch.cpp
... config.h(238): warning C4615: #pragma warning : unknown user warning type
It appears there is no enable or on for pragma warning. There is a once, but I want to see all instances of the potential problems, and not just one.
Microsoft's documentation on warning does not appear to discuss the topic of enabling warnings.
I can't really use # pragma warning (default: ...) because they are off by default. I don't want to modify each of the solution's configurations because the solution has 4 projects, 6 configurations and 4 platforms. So I have 80+ of them to change (the cross product of the selections).
How do I enable select warnings through the pragma?

VS 2008 Compiler option for flagging uninitialized variables

Is there a compiler option in VS 2008 (C++) to expose uninitialized variables? I'm trying to debug a problem where the "release" build of a DLL does not work but the "debug" build of the DLL does work.
iirc, setting warning level to 4 will help with this
cl.exe sample.cpp /analyze
here's the link on MSDN
You're looking for warning number C6001
My normal debug builds seem to have this warning enabled:
warning C4700: uninitialized local variable 'xxx' used
and the warning is not present in a release build.
I don't know if this is still relevant but I was just looking for the same thing and found a solution.
You can manually change the warning-level for single warnings. In your case you have to set the level for this particular warning at least as low as your configured level of default-warnings (usually 1 or 2).
Within the project-settings within the C/C++ settings enter a manual command-line switch:
/wYxxxx
with Y being the warninglevel (e.g. 1)
and xxxx being the warningnumber in this case you should enter
/w14701
Cheers

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 )