'MS Build Tools MSVC compiler cl warnings suddenly disappear

I build my project with MS Build Tools. Previously, I had been using the default VS generator. But now I have tried nmake JOM Makefiles generator and get the warning cl : Command line warning D9025 : overriding '/W3' with '/W4 -- as I have cmake_minimum_required(VERSION 3.14) in my CMakeLists.txt.

However, this warning appeared only on the first build. Then it disappeared. I tried to clean the build of course and removed all the AppData/Local/Temp files. I encoutered this warning a couple of more times when building my project but could not spot the cue. What can be thereason of such a strange behvior. Is there any cache some where for MS Build Tools?

I need to understand this to be able to correct my CMakeLists.txt and check that nobody gets the warning when building my project.

UPDATE:

I noticed that this happens when I change something in my CMakeLists.txt and then run cmake --build <dir>.

Then I get:

cl   ... /DWIN32 /D_WINDOWS /W3 /GR /EHsc /EHsc /W4 /WX /Zi /Ob0 /Od /RTC1 -MTd   -UUNICODE -U_UNICODE /showIncludes /FoCMakeFiles\project.dir\main.cpp.obj /FdCMakeFiles\project.dir\ /FS -c C:\project\main.cpp
cl : Command line warning D9025 : overriding '/W3' with '/W4'

My project contains /EHsc /W4 /WX flags.

However, I do not add /W3 /GR and an extra /EHsc anywhere.



Solution 1:[1]

The reason that I get the warning only some times was that I used

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc /W4 /WX")

rather than add_compile_options(/EHsc /W4 /WX).

I am not sure why. Maybe, somebody could comment on this or write better answer.

Anyway, when I used add_compile_options, the warning persisted, i.e. it appeared all the time -- as expected and more understandable.

I got rid of it by using

string(REGEX REPLACE "/W[1-3]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

as suggested here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JenyaKh