[CMake] [MSVC] Setting warning level on target feels like long-time bug

Craig Scott craig.scott at crascit.com
Sun Dec 9 06:14:22 EST 2018


>From what I understand from a very limited quick search just now, it seems
that /W3 is the default warning level for Visual Studio (according to
the Microsoft
docs
<https://docs.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level?view=vs-2017>),
but CMake explicitly adds it
<https://gitlab.kitware.com/cmake/cmake/blob/master/Modules/Platform/Windows-MSVC.cmake#L360>
as a default compiler flag in CMAKE_<LANG>_FLAGS_INIT. This makes me wonder
if it has always been the default, otherwise it isn't clear why it was
deemed necessary to add it. More to the point, unless there's a reason not
to, perhaps we could consider removing it from the default flags CMake
sets. I think this would largely address the situation you're describing
and shouldn't actually change the behavior of existing projects. I've CC'ed
the developer's list and suggest that follow-up discussion should occur
there.


On Sun, Dec 9, 2018 at 8:07 AM Mateusz Loskot <mateusz at loskot.net> wrote:

> Hi,
>
> I define a target for a library, and set warning level for MSVC compiler:
>
> target_compile_options(mylib PRIVATE $<$<CXX_COMPILER_ID:MSVC>:-W4>)
>
> Although this works well, the compiler throws this warning:
>
> cl : Command line warning D9025: overriding '/W3' with '/W4'
>
> I want to get rid of this warning, so I fix it this way
>
> if(MSVC)
>   string(REGEX REPLACE "/W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
>   string(REGEX REPLACE "-W3" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
> endif()
>
> target_compile_options(mylib PRIVATE $<$<CXX_COMPILER_ID:MSVC>:-W4>)
>
> This works like charm, but leaves me with sour feeling that
> something is not right here.The REGEX REPLACE clean-up
> has become such a habit, it's almost canonical thing I do in
> all my CMake-based projects.
>
> Shouldn't CMake drop the default when target_compile_options is called?
> Is this behaviour by design, for MSVC?
> Could anyone help me to understand if this is actually a bug
> or am I misunderstanding anything?
>
> Best regards,
> --
> Mateusz Loskot, http://mateusz.loskot.net
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
>


-- 
Craig Scott
Melbourne, Australia
https://crascit.com

New book released: Professional CMake: A Practical Guide
<https://crascit.com/professional-cmake/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20181209/34994c15/attachment.html>


More information about the CMake mailing list