Disable the warning of specific libraries by cmake

10,027

Solution 1

It is not possible to do that by CMake because such thing is not possible in MSVC. But you can disable warnings in your source code using pragma directive. You will need to identify which header they are coming from, the warning number and disable the warning for that header only. For example:

#ifdef _MSC_VER
#pragma warning(disable: 4345) // disable warning 4345
#endif
#include <boost/variant.hpp>
#ifdef _MSC_VER
#pragma warning(default: 4345) // enable warning 4345 back
#endif

Solution 2

You can disable specific warnings while building any target if you set the relevant compiler flags to disable them with calls to target_compile_options (reference).

For example, say you want to disable C4068: unknown pragma 'mark' warnings in Visual Studio 2019 when you're building target foo. The Visual Studio compiler options to generate warnings mentions that the flag /wdnnnn suppresses warning nnnn. Therefore, to suppress that warning you update your CMakeLists.txt file with the following command.

if(MSVC)
    target_compile_options(foo 
        PRIVATE
            "/wd4068;" # disable "unknown pragma 'mark'" warnings
    )
endif()
Share:
10,027
user3689849
Author by

user3689849

Updated on June 06, 2022

Comments

  • user3689849
    user3689849 almost 2 years

    I am using boost, Qt and other libraries to develop some applications and using cmake as my make tool. To eliminate the problems earlier, I decided to turn on the strongest warning flags(thanks for mloskot)

    if(MSVC)
      # Force to always compile with W4
      if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
        string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
      else()
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
      endif()
    elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR
    "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
      # Update if necessary
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
    endif()
    

    So far so good, but this would trigger a lot of warnings about the libraries i am using too, is it possible to disable the warnings of specific folders, files or libraries by cmake?

    Edit : I am talking about the usage of the 3rd party libraries.The examples are

    G:\qt5\T-i386-ntvc\include\QtCore/qhash.h(81) : warning C4127: conditional expression is constant
    
    G:\qt5\T-i386-ntvc\include\QtCore/qlist.h(521) : warning C4127: conditional expression is constant
            G:\qt5\T-i386-ntvc\include\QtCore/qlist.h(511) : while compiling class template member function 'void QList<T>::append(const T &)'
            with
            [
                T=QString
            ]
            G:\qt5\T-i386-ntvc\include\QtCore/qstringlist.h(62) : see reference to class template instantiation 'QList<T>' being compiled
            with
            [
                T=QString
            ]
    

    and so on

  • drescherjm
    drescherjm almost 9 years
    This is exactly what I was thinking of when I made my comment on the question.
  • Sandburg
    Sandburg over 4 years
    there is also a syntax with push / pop: #pragma warning(push) #pragma warning(disable : 4345) #pragma warning(pop)