SebastianK
SebastianK

Reputation: 3707

Declare qt headers as system headers with CMake

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

Upvotes: 5

Views: 889

Answers (2)

Loris
Loris

Reputation: 771

Here's another simpler solution with CMake version 3 and Qt6 (tailor it to your needs):

find_package(Qt6 COMPONENTS Quick REQUIRED)

add_executable(main main.cpp)
target_compile_options(main PRIVATE -Werror -Wall -Wextra)

# Mark Qt6 include directories as SYSTEM include directories
target_include_directories(main SYSTEM PRIVATE ${Qt6Quick_INCLUDE_DIRS})

target_link_libraries(main PRIVATE Qt6::Quick)

Upvotes: 0

ComicSansMS
ComicSansMS

Reputation: 54589

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)

Upvotes: 4

Related Questions