Skip to content

Commit 96a33a9

Browse files
Deprecate gtest_force_shared_crt
CMake 3.15 added MSVC_RUNTIME_LIBRARY, an official mechanism to configure the runtime library used by targets. Setting the property will properly set the MSVC runtime library. With MSVC_RUNTIME_LIBRARY available, gtest_force_shared_crt is no longer necessary and is deprecated, with a notice given.
1 parent 1b96fa1 commit 96a33a9

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

googletest/CMakeLists.txt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
# ctest. You can select which tests to run using 'ctest -R regex'.
99
# For more options, run 'ctest --help'.
1010

11-
# When other libraries are using a shared version of runtime libraries,
12-
# Google Test also has to use one.
13-
option(
14-
gtest_force_shared_crt
15-
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
16-
OFF)
11+
if (DEFINED gtest_force_shared_crt)
12+
message(DEPRECATION
13+
"gtest_force_shared_crt is deprecated and ignored. Use `CMAKE_MSVC_RUNTIME_LIBRARY` to configure the MSVC runtime library")
14+
endif()
1715

1816
option(gtest_build_tests "Build all of gtest's own tests." OFF)
1917

googletest/README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
117117
detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
118118
'MDd_DynamicDebug' in main.obj
119119

120-
GoogleTest already has a CMake option for this: `gtest_force_shared_crt`
120+
This is resolved by forcing googletest to dynamically link to the C runtimes.
121+
CMake 3.15 added the variable `CMAKE_MSVC_RUNTIME_LIBRARY` as the way to
122+
select the C runtime used by projects.
123+
124+
To do this, either add
125+
`set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")`
126+
to the project's CMakeLists.txt before including googletest, or set it
127+
on the command line with
128+
`-DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"`.
121129

122-
Enabling this option will make gtest link the runtimes dynamically too, and
123-
match the project in which it is included.
124130

125131
#### C++ Standard Version
126132

googletest/cmake/internal_utils.cmake

Lines changed: 13 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,6 +61,19 @@ macro(config_compiler_and_linker)
7061
endif()
7162

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

0 commit comments

Comments
 (0)