I have an external library which I'm using in my package
- package/
- include/
- src/
- external_library/
- CMakeLists.txt
- CMakeLists.txt
and currently in package/CMakeLists.txt I add the external library by
add_subdirectory(external_library library_name)
add_executable(my_executable src/main.cpp)
target_link_libraries(my_executable library_name)
and within external_library/CMakeLists.txt there is something like
add_library(library_name <files>)
I'd like to disable warnings from files in external_library/ so I can change the error settings in package/CMakeLists.txt without seeing conflicts from the external library. I would like to avoid changing anything in external_library/ if at all possible. How can that be done?
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 #included 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With