Compare commits

..

2 Commits

Author SHA1 Message Date
Charles Giessen
ac40ea7af3
Merge 0b656495436f57212be8700794a3d5fb14000c5f into 1b96fa13f549387b7549cc89e1a785cf143a1a50 2025-12-03 14:53:14 -07:00
Charles Giessen
0b65649543 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.
2025-12-03 15:52:55 -06:00
3 changed files with 20 additions and 25 deletions

View File

@ -8,10 +8,12 @@
# ctest. You can select which tests to run using 'ctest -R regex'.
# For more options, run 'ctest --help'.
if (DEFINED gtest_force_shared_crt)
message(DEPRECATION
"gtest_force_shared_crt is deprecated and ignored. Use `CMAKE_MSVC_RUNTIME_LIBRARY` to configure the MSVC runtime library")
endif()
# When other libraries are using a shared version of runtime libraries,
# Google Test also has to use one.
option(
gtest_force_shared_crt
"Use shared (DLL) run-time lib even when Google Test is built as static lib."
OFF)
option(gtest_build_tests "Build all of gtest's own tests." OFF)

View File

@ -117,16 +117,10 @@ something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
'MDd_DynamicDebug' in main.obj
This is resolved by forcing googletest to dynamically link to the C runtimes.
CMake 3.15 added the variable `CMAKE_MSVC_RUNTIME_LIBRARY` as the way to
select the C runtime used by projects.
To do this, either add
`set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")`
to the project's CMakeLists.txt before including googletest, or set it
on the command line with
`-DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>DLL"`.
GoogleTest already has a CMake option for this: `gtest_force_shared_crt`
Enabling this option will make gtest link the runtimes dynamically too, and
match the project in which it is included.
#### C++ Standard Version

View File

@ -62,19 +62,18 @@ macro(config_compiler_and_linker)
fix_default_compiler_settings_()
# When Google Test is built as a shared library, it should also use
# shared runtime libraries. Otherwise, it may end up with multiple
# copies of runtime library data in different modules, resulting in
# hard-to-find crashes. When it is built as a static library, it is
# preferable to use CRT as static libraries, as we don't have to rely
# on CRT DLLs being available. CMake defaults to using shared
# CRT libraries, so we change that here.
if (NOT BUILD_SHARED_LIBS AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
if (MSVC)
# When Google Test is built as a shared library, it should also use
# shared runtime libraries. Otherwise, it may end up with multiple
# copies of runtime library data in different modules, resulting in
# hard-to-find crashes. When it is built as a static library, it is
# preferable to use CRT as static libraries, as we don't have to rely
# on CRT DLLs being available. CMake defaults to using shared
# CRT libraries, so we change that here.
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt AND NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
# Newlines inside flags variables break CMake's NMake generator.
# TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.
set(cxx_base_flags "-GS -W4 -WX -wd4251 -wd4275 -nologo -J")