Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake project in Visual Studio gives flag override warnings (Command line warning D9025: overriding '/W4' with '/w')

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?

like image 394
Serban Stoenescu Avatar asked Dec 31 '25 05:12

Serban Stoenescu


1 Answers

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}")
like image 100
Kevin Avatar answered Jan 02 '26 01:01

Kevin