You could manually add the -w
flag for each external target, which disables all warnings on GCC, Clang, and MSVC (or whatever flag for your compiler for a specific warning you want to disable), and rely on the fact that later flags will override previous flags for those compilers. Ex. To disable all warnings for the compilation of a target, you'd use target_compile_options(foo PRIVATE -w)
.
If there are a lot of targets in the external project and you want to suppress warnings for all of them in a similar fashion, assuming you don't want to modify the external project's CMakeLists.txt files, you can save a copy of the COMPILE_OPTIONS
directory property, modify it, add the subdirectory, and then restore the old value. Ex.
# retrieve a copy of the current directory's `COMPILE_OPTIONS`
get_directory_property(old_dir_compile_options COMPILE_OPTIONS)
# modify the actual value of the current directory's `COMPILE_OPTIONS` (copy from above line remains unchanged). subdirectories inherit a copy of their parent's `COMPILE_OPTIONS` at the time the subdirectory is processed.
add_compile_options(-w)
# add you subdirectory (whichever way you do it)
# add_subdirectory(external ...)
# FetchContent_MakeAvailable(...)
# restore the current directory's old `COMPILE_OPTIONS`
set_directory_properties(PROPERTIES COMPILE_OPTIONS "${old_dir_compile_options}")
If the warnings are in the library's headers and they're happening when you include the headers, try marking the target as SYSTEM
(which you can do with cmake_minimum_required(VERSION 3.25)
or higher by using the SYSTEM
target property). Pre-CMake 3.25, you can use a workaround in which you copy/move the INTERFACE_INCLUDE_DIRECTORIES
target property to the INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
target property (related post). There may be other ramifications to doing this, but one of the side-effects of marking a target as SYSTEM
is usually that the compiler will not emit warnings for them when #include
d by something that has warning flags.
If you have cmake_minimum_required(VERSION 3.25)
or higher and the external library is header-only, you can consider a different solution. add_subdirectory
and FetchContent_Declare
come with a SYSTEM
option that you can use, which causes all targets added in the corresponding subdirectory and under to have their include directories all treated as being SYSTEM
(which will result in CMake generating a corresponding include flag for the compiler).