googletest
googletest copied to clipboard
googletest does not respect CMake's CMP0091 policy.
Describe the bug
googletest relies on CMake setting the /MD
compiler flag as a default
https://github.com/google/googletest/blob/9c332145b71c36a5bad9688312c79184f98601ff/googletest/cmake/internal_utils.cmake#L40
However, if CMP0091 is set to NEW
this doesn't happen anymore.
Because of this, googletest will always use the dynamic version of the CRT and as such, errors like this:
gtest.lib(gtest-all.cc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in main.cpp.obj
when linking against something that uses the static version of the CRT.
This behavior happens regardless of whether gtest_force_shared_crt
is set to OFF or ON (or set at all), because the string(REPLACE ...) function used doesn't replace anything because the expected /MD flag is not present in any of the flags inspected by the fix_default_compiler_settings_
macro.
My personal temporary fix was doing
set_property(TARGET gtest PROPERTY MSVC_RUNTIME_LIBRARY "<proper msvc runtime library here>")
set_property(TARGET gtest_main PROPERTY MSVC_RUNTIME_LIBRARY "<proper msvc runtime library here>")
after the FetchContent_MakeAvailable(googletest)
call.
This makes it so the targets now respect the setting given by CMP0091. This also completely overrides the need to set gtest_force_shared_crt
.
Steps to reproduce the bug
Clone https://github.com/Atari2/SpriteToolSuperDelux Switch to gtest-fail-link branch Run
mkdir build
cd build
cmake -S .. -DMSVC_STATIC_STL=ON
cmake --build . --config Debug --target PixiUnitTest
and observe the linking bug between harness.cpp.obj and gtest-all.obj.
Does the bug persist in the most recent commit?
Yes
What operating system and version are you using?
Windows 10, 22H2 build 19045.2251
What compiler and version are you using?
Microsoft (R) C/C++ Optimizing Compiler Version 19.34.31933 for x64
What build system are you using? cmake version 3.24.2