Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable specific warning inherited from parent in Visual Studio?

Suppose I am working on a large code base that has warning w44101 enabled by default. Meaning if I go into my project and right click properties -> C/C++ -> Command Line -> /w44101 shows up in the additional options section.

I want to be able to disable this warning by altering the configuration instead of the source code. I tried going into properties -> C/C++ -> All Options -> Disable Specific Warnings and put in 4101, and this actually produces a /wd"4101" in properties -> C/C++ -> Command Line. However, when I compile my project, it still throws the 4101 warning. Why doesn't /wd"4101" and /w44101 cancel out each other?

I am on Windows 10 with Visual Studio 2015. What is the correct way to disable this warning? It will be preferable if the proposed solutions could be invoked with some type of function in CMake since the .sln file of this code base is generated by CMake.

EDIT: This code base I am working on has a strict compile flag setup by default. It is compiled with /W4 and /WX. Also additional level 4 warnings, to name a few as an example, /w44101, /w44062, /w44191 etc.

like image 314
user3667089 Avatar asked Dec 18 '16 05:12

user3667089


People also ask

How do I supress a warning in C#?

Use a #pragma warning (C#) or Disable (Visual Basic) directive to suppress the warning for only a specific line of code.

How do I turn off error warnings?

You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).

How do I turn off Cmake warnings?

Use -Wno-dev to suppress it. You can disable the warning like this when you are configuring your build.


1 Answers

In your question, you're enabling warning 44101 (which doesn't exist if I'm right?), but disabling warning 4101: is that a typo?

EDIT:

You answered this in the comments of your question. Reading MSDN documentation, /wlnnnn option allows to set the warning level at l for the warning number specified by nnnn. So /w44101 enables warning number 4101 at level 4.


Anyway, if your projects are generated with CMake, add_compile_options can be used to add options to the compilation of source files in the current directory. This can be used to enable the warning 4101 at "global scope" at warning level 4:

add_compile_options(/w44101)

You can then use target_compile_definitions to disable it per-target:

add_library(foo ...)
target_compile_definitions(foo PUBLIC /wd4101)

EDIT:

From your comments, in the head CMake file of the repo there is:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w44101")

And in your project CMake file you attempt to do:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4101")

What you should do is removing the /w44101 from CMAKE_CXX_FLAGS. You can achieve this using string(REPLACE ...) to replace /w44101 with an empty string:

string(REPLACE "/w44101" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

Obviously, the best solution would be to fix the code generating the warning. 4101 is about unused variables, that's easy to fix ;)

(see related question "How do I best silence a warning about unused variables?")

like image 124
rgmt Avatar answered Oct 04 '22 22:10

rgmt