0

my project warnlevel is /w3. I want to enable as single warning from warnlevel 4. Such as "C4296 'operator' : expression is always false"

How can I do this in cxx flags of my CMake file?

znoopy2k
  • 58
  • 5
  • In what sense is your Visual Studio project integrated with CMake? I am not aware that they fit together out of the box. http://stackoverflow.com/q/395169/560648 http://stackoverflow.com/q/4151908/560648 – Lightness Races in Orbit Sep 29 '15 at 16:19
  • 1
    See https://msdn.microsoft.com/en-us/library/thxezb7y.aspx *"For example, /w14326 causes C4326 to be generated as a level 1 warning."* – Richard Critten Sep 29 '15 at 16:26

2 Answers2

2

Just add to your main CMakeLists.txt (CMake version >= 2.8.12) generally

add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/w34296>")

or (thanks @tambre for the hint) just to a specific target

target_add_compile_options(MyTarget "$<$<CXX_COMPILER_ID:MSVC>:/w34296>")

The shwon generator expression will add warning C4296 to warning level 3 for MSVC compilers (for the VC warning options see link provided by @Richard Critten)

Some more possibilities for setting compile option with CMake in Visual Studio projects can be found in my answer given here.

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149
-2

This code works for me.

#enable compiler warning C4296 in warnlevel 3
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /w34296 ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /w34296 ")
znoopy2k
  • 58
  • 5
  • This is quite terrible, as it sets the option for *all* other compilation units also in your project. Those variables should no longer be used in modern CMake. – tambre May 01 '17 at 12:53