Search code examples
c++visual-studiocmakecompiler-warningssuppress-warnings

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.


Solution

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