CMake GoogleTest override flags
GoogleTest internally set CMake compiler flags are too aggressive for end users, and can cause build errors. We experienced this with Intel oneAPI, and created a workaround for the GoogleTest-consuming CMake project to override the offending flags. This technique is useful in general with third-party CMake projects, including those obtained by FetchContent.
For GoogleTest, we determined that Intel oneAPI was experiencing nusiance internal errors:
- Windows “-WX” flag
- Linux “-Werror” flag
We overrode those flags with this CMake script:
FetchContent_MakeAvailable(googletest)
if(CMAKE_CXX_COMPILER_ID STREQUAL "IntelLLVM")
foreach(t IN ITEMS gtest gtest_main gmock gmock_main)
if(WIN32)
# necessary since GoogleTest injects /WX blindly and that fails builds with modern IntelLLVM.
# https://learn.microsoft.com/en-us/cpp/build/reference/compiler-option-warning-level
target_compile_options(${t} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/WX->)
else()
# necessary to avoid
# error: unknown warning option '-Wno-implicit-float-size-conversion' [-Werror,-Wunknown-warning-option]
target_compile_options(${t} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Wno-error=unknown-warning-option>)
endif()
endforeach()
endif()The override “/WX-” nullifies the /WX flag that errors for nuisance warnings internal to GoogleTest. The “-Wno-” flag is the same as the underlying GCC compiler on Linux.