Merge 96a33a9035b8fcbc263a73a14d4d3e8897101499 into 1b96fa13f549387b7549cc89e1a785cf143a1a50

This commit is contained in:
Charles Giessen 2025-11-26 22:08:30 +00:00 committed by GitHub
commit 82d2283459
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 18 deletions

View File

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

View File

@ -117,10 +117,16 @@ something like the following: gtest.lib(gtest-all.obj) : error LNK2038: mismatch
detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value
'MDd_DynamicDebug' in main.obj 'MDd_DynamicDebug' in main.obj
GoogleTest already has a CMake option for this: `gtest_force_shared_crt` 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"`.
Enabling this option will make gtest link the runtimes dynamically too, and
match the project in which it is included.
#### C++ Standard Version #### C++ Standard Version

View File

@ -28,15 +28,6 @@ macro(fix_default_compiler_settings_)
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt) if (NOT BUILD_SHARED_LIBS AND NOT gtest_force_shared_crt)
# 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 always defaults to using shared
# CRT libraries, so we override that default here.
string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
# When using Ninja with Clang, static builds pass -D_DLL on Windows. # When using Ninja with Clang, static builds pass -D_DLL on Windows.
# This is incorrect and should not happen, so we fix that here. # This is incorrect and should not happen, so we fix that here.
string(REPLACE "-D_DLL" "" ${flag_var} "${${flag_var}}") string(REPLACE "-D_DLL" "" ${flag_var} "${${flag_var}}")
@ -70,6 +61,19 @@ macro(config_compiler_and_linker)
endif() endif()
fix_default_compiler_settings_() 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) if (MSVC)
# Newlines inside flags variables break CMake's NMake generator. # Newlines inside flags variables break CMake's NMake generator.
# TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds. # TODO(vladl@google.com): Add -RTCs and -RTCu to debug builds.