13

I have a CMake project, which I am building with Microsoft Visual Studio 2019. I am trying to fix and remove all warnings, but there is one type which I can't disable or fix.

All of them are of the type:

Command line warning D9025: overriding '/W4' with '/w'
Command line warning D9025: overriding '/W3' with '/W4'

I tried fixing them, but I can't find out what's causing all of them.

My question is:

How can I disable the warnings using CMake? Or is there a surefire way to find the underlying cause of them and fix them?

Kevin
  • 16,549
  • 8
  • 60
  • 74
Serban Stoenescu
  • 3,136
  • 3
  • 22
  • 41

2 Answers2

13

This issue has been raised (here and here), and depending on your CMake version there are a couple solutions.

When building for MSVC with CMake, the compiler warning flags (like /W3) are added by default. In CMake 3.15, CMake introduced a fix for this, and the compiler warning flags are no longer automatically added so the warning should no longer appear. From the docs:

CMake 3.15 and above prefer to leave out warning flags from the value of CMAKE_<LANG>_FLAGS by default.

Along with this fix, CMake introduced policy CMP0092, which allows you to switch back to the OLD behavior (adding the warning flags by default) if necessary.


If you are tied to a CMake version older than 3.15, you can manually manipulate the CMAKE_<LANG>_FLAGS variable to replace the warnings yourself using CMake's regular expressions. You can try something like this:

string(REGEX REPLACE "/W[3|4]" "/w" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • 4
    I am getting this issue even with CMake 3.19.2 on Visual Studio 2019 Pro edition. – varagrawal Jan 04 '21 at 17:08
  • 6
    This is probably because your minimum required CMake version is less than 3.15, you need to use at least 3.15 in your project: `cmake_minimum_required(VERSION 3.15)` – ABBAPOH May 12 '21 at 09:42
  • Do I need to explicitly define this flag in CMAKElist.txt "CMAKE__FLAGS" – Sijith Oct 27 '21 at 11:53
  • 2
    I tried to suppress warnings from external libraries with /external:W0 flag, and I am getting similar issue: "overriding /external:W3 with /external:W0". My cmake_minimum_required is 3.18. – Ivan_a_bit_Ukrainivan Dec 29 '21 at 12:23
  • 6
    Maybe it's obvious for some but I was having the issue persist after I've increased required version until I've done `Delete Cache and Reconfigure` from within msvc. Seems like `CMAKE_CXX_FLAGS` value still stays in the cache otherwise. – Predelnik May 07 '22 at 13:15
  • Is there a generic solution for other flags (like /Zi) or if you want to change the warning level in a subproject? Or you still need to resort to ugly string regex replace? – Dan M. Aug 27 '23 at 20:31
6

I had this problem even though I was using CMake 3.19. It was caused by having the project command before the cmake_minimum_required command.

Peter Allin
  • 81
  • 1
  • 2