googletest
googletest copied to clipboard
[Bug]: gtest_force_shared_crt on Windows no longer selects /MT with static library build option
Describe the issue
After updating to latest googletest on windows 11 with latest Visual C++ compiler - all googletest compile flags use "/MD" instead of desired "/MT" even though I have specified gtest_force_shared_crt.
Linker errors begin with:
gmock_main.lib(gtest-all.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in test-ErrInfo.obj [Y:\abruce-dev\Tc32\cpp\tc3_core\windows
\desktop\tc3_core_static\gtest\build\x64\test_tc3_core_static.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __cdecl std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QEAA@H@Z) already defined in libcpmtd.lib(xlock.obj) [Y:\abruce-dev\Tc32\cpp\tc3_core\windows\desktop\t
c3_core_static\gtest\build\x64\test_tc3_core_static.vcxproj]
Steps to reproduce the problem
Pull latest googletest and build:
git clone https://github.com/google/googletest.git
cd googletest ; mkdir -p build ; cd build
cmake \
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
-DCMAKE_GENERATOR_PLATFORM=x64 \
-Dgtest_force_shared_crt=1 -DBUILD_SHARED_LIBS=0 ..
cmake --build ..
To generate a link error - link the built googletest with any project that uses the static runtime library (/MT).
Please note: The error indicated by this issue is not technically the fact that linking the built googletest against any project using static runtime library fails. Rather the error is that googletest no longer selects the /MT static runtime library even though gtest_force_shared_crt is set. Each output compile line indicates that the DLL runtime library is selected (/MD). Example:
ClCompile:
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.43.34808\bin\HostX64\x64\CL.exe /c /I"Y:\abruce-dev\Tc32\work\gtest\windows\googletest\googletest\include" /I"Y:\abruce-dev\Tc32\work\
gtest\windows\googletest\googletest" /Zi /nologo /W4 /WX /diagnostics:column /Od /Ob0 /D _UNICODE /D UNICODE /D WIN32 /D _WINDOWS /D _UNICODE /D UNICODE /D _WIN32 /D STRICT /D WIN32_LEAN_AND_MEAN /D GTEST_HAS
_PTHREAD=0 /D _HAS_EXCEPTIONS=1 /D "CMAKE_INTDIR=\"Debug\"" /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /std:c++17 /Fo"gtest.dir\Debug\\" /Fd"Y:\abruce-dev\Tc32\work\gtest\windows
\googletest\build\lib\Debug\gtestpdb_debug_postfix-NOTFOUND.pdb" /external:W4 /Gd /TP /wd4251 /wd4275 /wd4702 /errorReport:queue -J -utf-8 "Y:\abruce-dev\Tc32\work\gtest\windows\googletest\googletest\src\gte
st-all.cc"
gtest-all.cc
What version of GoogleTest are you using?
l.abruce@ANDREWBRUCEEE6B /cygdrive/y/abruce-dev/Tc32/work/gtest/windows/googletest
$ pwd
/cygdrive/y/abruce-dev/Tc32/work/gtest/windows/googletest
l.abruce@ANDREWBRUCEEE6B /cygdrive/y/abruce-dev/Tc32/work/gtest/windows/googletest
$ git rev-parse HEAD
4ee4b17bf5ae1bf6cdb95693c174b8830898c00b
What operating system and version are you using?
l.abruce@ANDREWBRUCEEE6B /cygdrive/y/abruce-dev/Tc32/cpp/tc3_core/windows/desktop/tc3_core_static
$ systeminfo
Host Name: ANDREWBRUCEEE6B
OS Name: Microsoft Windows 11 Home
OS Version: 10.0.22631 N/A Build 22631
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
Registered Owner: Andrew Bruce
Registered Organization:
Product ID: 00326-10000-00000-AA546
Original Install Date: 8/29/2024, 9:11:51 AM
System Boot Time: 3/10/2025, 10:47:01 PM
System Manufacturer: Parallels International GmbH.
System Model: Parallels Virtual Platform
System Type: x64-based PC
Processor(s): 1 Processor(s) Installed.
[01]: Intel64 Family 6 Model 158 Stepping 13 GenuineIntel ~2400 Mhz
BIOS Version: Parallels International GmbH. 20.2.2 (55879), 2/22/2025
Windows Directory: C:\Windows
System Directory: C:\Windows\system32
Boot Device: \Device\HarddiskVolume2
System Locale: en-us;English (United States)
Input Locale: en-us;English (United States)
Time Zone: (UTC-05:00) Eastern Time (US & Canada)
Total Physical Memory: 8,173 MB
Available Physical Memory: 5,405 MB
Virtual Memory: Max Size: 11,501 MB
Virtual Memory: Available: 8,082 MB
Virtual Memory: In Use: 3,419 MB
Page File Location(s): C:\pagefile.sys
Domain: WORKGROUP
Logon Server: \\ANDREWBRUCEEE6B
Hotfix(s): 4 Hotfix(s) Installed.
[01]: KB5049624
[02]: KB5027397
[03]: KB5052094
[04]: KB5052107
Network Card(s): 1 NIC(s) Installed.
[01]: Parallels VirtIO Ethernet Adapter
Connection Name: Ethernet
DHCP Enabled: Yes
DHCP Server: 10.211.55.1
IP address(es)
[01]: 10.211.55.3
Hyper-V Requirements: A hypervisor has been detected. Features required for Hyper-V will not be displayed.
What compiler and version are you using?
Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34808 for x64
What build system are you using?
cmake version 3.30.5-msvc23
Additional context
I was able to hack around the problem.
After running:
cmake \
-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
-DCMAKE_GENERATOR_PLATFORM=x64 \
-Dgtest_force_shared_crt=1 -DBUILD_SHARED_LIBS=0 ..
I ran script to locate each *.vcxproj* and force the correct RuntimeLibrary selection:
# check for Windows and apply hack
if lcl_is_os_windows ; then
echo 'Force correct runtime library for Windows...'
for i in MultiThreadedDebugDLL:MultiThreadedDebug MultiThreadedDLL:MultiThreaded ; do
the_unwanted_runtime_library="`echo "$i" | awk -F':' '{print $1}' | dos2unix`"
the_desired_runtime_library="`echo "$i" | awk -F':' '{print $2}' | dos2unix`"
for j in `/bin/find -type f -name '*vcxproj' -exec grep -le $the_unwanted_runtime_library {} \;` ; do
set -x
sed -ie "s/\(RuntimeLibrary.*\)$the_unwanted_runtime_library/\1$the_desired_runtime_library/" "$j" || exit $?
set +x
# remove backup file auto-created by sed
rm -f "$j"e
done
done
echo ''
fi
After applying the hack building googletest then shows that /MT is selected as desired and my tests pass.
I have attached the log from a failing run. The log includes all setup commands (zero-to-hero gtest build), compile / link phases for gtest, compile / link for my test project, and the failing linker output.
Hope this helps!