Issue
Note: This question is similar to an existing question about FetchContent
, which under the hood calls add_subdirectory
, but I wouldn't call it a duplicate question.
When calling target_include_directories
, one can specify the SYSTEM
argument to make the include directories treated as system headers, which (for most compilers) has useful compiler implications for things like header resolution order and whether warning checks should be evaluated on includes of those headers. But the CMake scripts I add via add_subdirectory
are not always ones authored by me- especially when using libraries that I didn't write. In those cases, I can't (more specifically- don't want to go through the manual process to) change the scripts to make their calls to target_include_directories
specify SYSTEM
, and it wouldn't make sense for the library author to specify SYSTEM
at the source, since the headers of a library at its source are not system headers.
Is there a way in current or future CMake versions to make targets added from a call to add_subdirectory
have their include directories all be specified with SYSTEM
?
Solution
CMake 3.25+
add_subdirectory
has a SYSTEM
argument much like include_directories
and target_include_directories
. This feature was contributed in merge request #7399 by daquexian.
I will update this post with links to official docs once they have been published.
Before CMake 3.25
I don't think there is a pretty way to do this pre-CMake 3.25. You can use a workaround to copy/move the contents of the INTERFACE_INCLUDE_DIRECTORIES
property of a target to its INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
property. This workaround is described in this answer by Florian Berchtold. Ex:
get_target_property("${target}_iid" "${target}" INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties("${target}" PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${target}_iid")
Answered By - david-fong Answer Checked By - Mary Flores (WPSolving Volunteer)