Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare qt headers as system headers with CMake

Tags:

c++

cmake

qt

I use CMake with qt by saying:

find_package(Qt5 COMPONENTS Widgets)

Also, I want to use a high warning level and I want treat warnings as errors. So I use:

set( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra" )

However, I do not care about warnings in the libraries I use. So, for example, to include boost I prepend SYSTEM in the include_directories call, so I do not get bothered by warnings from an external library:

include_directories(SYSTEM ${Boost_INCLUDE_DIR} )

But this does not work for qt, since there is no explicit include_directories statement where I could prepend SYSTEM.

Is there anything I could do about that? I only found a request for that feature here: http://www.itk.org/Bug/print_bug_page.php?bug_id=8710

like image 475
SebastianK Avatar asked Oct 03 '22 20:10

SebastianK


1 Answers

The easiest solution here is to set the warning level on a per-file basis instead of the global CMAKE_CXX_FLAGS. That way, only your own code gets the higher warning levels and you don't have to care about third-party code.

You can use the COMPILE_FLAGS property, which is available as both a target and file property:

set_property(TARGET <your_target_goes_here> 
             APPEND PROPERTY COMPILE_FLAGS "-Werror -Wall -Wextra")

The disadvantage is that you have to repeat this line for every of your targets, so you might want to wrap that into a CMake function for convenience.

If you are using CMake version 3 or higher, you should use target_compile_options instead of setting the property by hand:

target_compile_options(<your_target_goes_here> PRIVATE -Werror -Wall -Wextra)
like image 170
ComicSansMS Avatar answered Oct 13 '22 09:10

ComicSansMS