Issue
I have a project in witch I would like to use -Werror
, lets call its directory proj
. There is a directory within proj/external
and that is an exception so I don't want to use the -Werror
for that.
Is there a way to create an exception for an entire directory in cmake
for using or not using a compiler argument?
Solution
If you are adding the external directory via add_subdirectory
or FetchContent
In the CMakeLists.txt file at the subdirectory for proj
, do
# add `-Werror` to the current directory's `COMPILE_OPTIONS`
add_compile_options(-Werror)
# 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(-Wno-error)
# 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}")
Docs:
If you are adding it via ExternalProject_Add
You probably don't need to do anything unless the external project itself is adding -Werror
, in which case I don't know if you can do anything about it.
Obligatory caveats / comments:
-Werror
is a flag for gcc and friends (clang, etc.). If you want to support MSVC, you need to put guards in either viaif(...)
, or via generator expressions.- whether or not to use
-Werror
is not without controversy. If you want to support other users using your project and you want to satisfy both sides of the debate, use some mechanism to either guard these lines of configuration behind a CMake options, or isolate them to your local build only.
Answered By - david fong Answer Checked By - Mildred Charles (WPSolving Admin)