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

25.8k views Asked by At

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.

2

There are 2 answers

7
rocambille On BEST ANSWER

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?")

2
yano On

I googled "cmake disable visual studio warning", which brought me here. That's not exactly the question the OP is asking, but if this is the top hit...

add_compile_options(/wd<warningNumber>) didn't work for me (CMake version 3.21.3). In fact, add_compile_options wasn't even syntax-highlighted in notepad++. Instead, I found this page all the way back from 2008. It suggested using add_definitions instead, which did work for me:

...
if(WIN32)
  add_definitions("/wd4251")
endif(WIN32)

I don't normally advocate ignoring warnings, but in this particular case, I am building against several 3rd party and supplier DLLs, and as far as I can tell, warning 4251 doesn't necessarily indicate a problem. It's simply saying, "all these functions you're calling need to have a DLL-interface (I already knew that), so make sure you're doing this right". This warning floods the output, making it difficult to spot other, more important warnings, so I decided to disable it.