12

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.

user3667089
  • 2,996
  • 5
  • 30
  • 56
  • Look at the verbose build log. Which of the two options comes last in the compilation command? If parent puts it in the commandline section, you probably want to put it in the commandline section too, not in the "disable specific warnings" section. – n. m. could be an AI Dec 18 '16 at 10:33
  • @n.m. You are right... If I manually move `wd4101` to additional options AFTER `w44101` it actually compiles. Now I just have to figure out how to do this in CMake. If you know please don't hesitate to write an answer and I will accept it. – user3667089 Dec 18 '16 at 16:21
  • Fix your 4101 vs 44101 inconsistencies. If you cannot fix your question, then we don't want to waste time helping you as you prove that you don't listen to our comments. – Phil1970 Dec 18 '16 at 19:30
  • 2
    @Phil1970 Did you even try it yourself? If you put in `/w4101` instead of `/w44101` it won't even compile. The first `4` is indicating it's from warning level 4. – user3667089 Dec 18 '16 at 19:33
  • I didn't know about `/w4` for the warning level. In fact, I always use Visual Studio default and if there are some warnings that remain in code, I would use a pragma directly in that file. So obviously, `/wd4101` and `/w44101` would not cancel one another as the last one is depending on the error level, an information you did not provide in your question. So if you compile at level 3 or less, you would never see the warning with both options. – Phil1970 Dec 18 '16 at 19:52
  • `/w14101` would be the opposite of `/wd4101` provide you compile at least at warning level 1. From documentation, it does not seems possible to always enable a warning if we compile at level 0. **All testing I have done works as explained in documentation.** The only reason I see that you would get that warning if you disable it at project level (and have no other conflicting option) would be that the warnings are configured specifically for a given file. In any case, there are not enough information in the question to understand your problem. – Phil1970 Dec 18 '16 at 20:10

2 Answers2

14

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

Marc Durdin
  • 1,675
  • 2
  • 20
  • 27
rocambille
  • 15,398
  • 12
  • 50
  • 68
  • 4101 was used as an example because it is easy to reproduce. In the head CMakeList of the repo there is `set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w44101")`, and in my project CMakeList I attempt to do`set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4101")` but it didn't work. I interchanged them with your `add_compile_options` and `target_compile_definitions` but still the warning is threw. Does it work on your end? – user3667089 Dec 18 '16 at 16:14
  • @user3667089 I never disable warnings, so I didn't test it ;) I edited my answer with an other proposition, based on your additional information. Again, the best solution would be to fix the code. Warnings may point unreliable code and should be treated as errors – rocambille Dec 18 '16 at 18:30
  • I tried `list(REMOVE_ITEM CMAKE_CXX_FLAGS /w44101)` but it still threw the warning for me. Does it work on your end? – user3667089 Dec 18 '16 at 19:12
  • Inspired by your approach I actually figured out something that works. `string(REGEX REPLACE "/w44101" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")`. Please edit your answer to put this in and I will accept it! – user3667089 Dec 19 '16 at 00:08
  • 1
    @user3667089 I edited my answer. Correct me if I'm wrong, but you're not using any regular expression in the match string, so you can just use `string(REPLACE "/w44101" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")` – rocambille Dec 19 '16 at 09:32
  • I don't think that `target_compile_definitions(foo PUBLIC /wd4101)` is correct, at least on recent (3.15) cmake it should be `target_compile_options(foo PUBLIC /wd4101)`. – ajc Jul 22 '19 at 08:05
  • Really bad answer. It's long and wrong. – Jimmy Chen Dec 14 '21 at 03:18
4

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.

yano
  • 4,827
  • 2
  • 23
  • 35