Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable the warning of specific libraries by cmake

Tags:

c++

boost

cmake

qt

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

like image 590
user3689849 Avatar asked May 20 '15 03:05

user3689849


2 Answers

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
like image 83
doqtor Avatar answered Sep 19 '22 07:09

doqtor


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()
like image 24
RAM Avatar answered Sep 21 '22 07:09

RAM