Skip to content

Commit 0b65649

Browse files
Fix gtest_force_shared_crt in CMake 3.15+
With CMake 3.15, a new mechanism was added to control which CRT libraries link to. Because the minimum CMake version is above 3.15, googletest can leverage it in the implementation of gtest_force_shared_crt. If CMAKE_MSVC_RUNTIME_LIBRARY is already defined, googletest will respect its value rather than replace it, allowing for users to customize the exact CRT used by googletest.
1 parent 1b96fa1 commit 0b65649

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

googletest/cmake/internal_utils.cmake

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,6 @@ macro(fix_default_compiler_settings_)
2828
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
2929
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
3030
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
31-
# When Google Test is built as a shared library, it should also use
32-
# shared runtime libraries. Otherwise, it may end up with multiple
33-
# copies of runtime library data in different modules, resulting in
34-
# hard-to-find crashes. When it is built as a static library, it is
35-
# preferable to use CRT as static libraries, as we don't have to rely
36-
# on CRT DLLs being available. CMake always defaults to using shared
37-
# CRT libraries, so we override that default here.
38-
string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
39-
4031
# When using Ninja with Clang, static builds pass -D_DLL on Windows.
4132
# This is incorrect and should not happen, so we fix that here.
4233
string(REPLACE "-D_DLL" "" ${flag_var} "${${flag_var}}")
@@ -70,7 +61,19 @@ macro(config_compiler_and_linker)
7061
endif()
7162

7263
fix_default_compiler_settings_()
64+
7365
if (MSVC)
66+
# When Google Test is built as a shared library, it should also use
67+
# shared runtime libraries. Otherwise, it may end up with multiple
68+
# copies of runtime library data in different modules, resulting in
69+
# hard-to-find crashes. When it is built as a static library, it is
70+
# preferable to use CRT as static libraries, as we don't have to rely
71+
# on CRT DLLs being available. CMake defaults to using shared
72+
# CRT libraries, so we change that here.
73+
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
74+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
75+
endif()
76+
7477
# Newlines inside flags variables break CMake's NMake generator.
7578
# TODO([email protected]): Add -RTCs and -RTCu to debug builds.
7679
set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J")

0 commit comments

Comments
 (0)