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

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.

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

Clang: Override all warning and warning-as-error flags specified earlier on the command line

With gcc I can throw a bunch of -Wsome-issue and -Werror=other-issue flags on the command line and the later cancel them all with a single -w flag somewhere near the end.
This doesn't appear to be the case with clang. A trailing -w suppresses some warnings, but not others. See here for an example. I know I can manually disable each warning individually via -Wno-some-issue and -Wno-error=other-issue, but that's a real pain to manage in the long term.
Am I missing something? Is there a way to cancel all earlier warning flags? Is there a reason why -w can suppress some warnings but not others?
Background: My specific use case is a library containing a mix of source files. Some new and some ancient, semi-third-party stuff that we never want to look at, let alone edit. The project has some semi-strict warning flags set globally, but for these few files I'd like to override the global flags and disable all warnings and warnings-as-werrors. In CMake this is done by settings the COMPILE_OPTIONS property for those files, which appends the given flags after the global flags. With gcc this works just fine, but with clang it's proving to be a headache.
(And yes I know I could reorganise the project to force those files to be compiled into a separate target, but I was hoping to avoid that.)
The flag you need is -Wno-everything.
Godbolt: https://godbolt.org/g/33uABD

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.

Werror ignore certain files

I am wondering if it is possible to have werror in gcc/g++ exclude certain files (ones that I do not have source code to modify) so that I can continue using werror in a uninhibited state.
Use pragma directives with a newer (4.2 I think) version of gcc to turn off -Werror for certain headers prior to including them.
You might want to accept answers for your previous questions.
My only thought is to compile the files you can modify separately with -Werror and then link them with the other object/library files without -Werror.
#Sam Miller already gave the reference documentation about how to do this...
You can temporarily disable -Werror on certain warnings with #pragma GCC diagnostics warning "-W<kind>". For example:
#pragma GCC diagnostic push
# pragma GCC diagnostic warning "-Wreturn-type"
# pragma GCC diagnostic warning "-Wmissing-braces"
# include "legacy-crap.h"
#pragma GCC diagnostic pop
Newer gcc will print the name of the diagnostics category in brackets as part of the the warning/error:
warning-test.c:11:1: warning: return type defaults to ‘int’ [-Wreturn-type]
or
warning-test.c:11:1: error: return type defaults to ‘int’ [-Wreturn-type]
This can be used to accurately select the exact diagnostics which should be treated as warning instead of error during processing of the third party crap you have no power to change. I do not know a short hand to disable all the warnings ("-Wall" will not have desired effect for the above #pragma), but I think it is also good to be explicit here.

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 )