[Bug]: Failed to build shared lib with Clang20 on Windows
- Dominant language
- C++
- Stars
- 39.5k
- Forks
- 10.9k
- Avg merge
- 6d 13h
- Merged PRs (30d)
- 1
Description
### Describe the issue
When setting the cmake variable `BUILD_SHARED_LIBS` `ON`, and using Clang as compiler, the building failed:
```
❯ cmake --build build
[8/8] Linking CXX shared library bin\gmock_main.dll
FAILED: [code=1] bin/gmock_main.dll lib/gmock_main.lib
C:\Windows\system32\cmd.exe /C "cd . && "D:\Program Files\LLVM\bin\clang++.exe" -nostartfiles -nostdlib -D_DLL -D_MT -Xclang --dependent-lib=msvcrt -fuse-ld=lld-link -shared -o bin\gmock_main.dll -Xlinker /MANIFEST:EMBED -Xlinker /implib:lib\gmock_main.lib -Xlinker /pdb:bin\gmock_main.pdb -Xlinker /version:1.16 googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj lib/gmock.lib lib/gtest.lib -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -loldnames && cd ."
lld-link: error: undefined symbol: class testing::internal::Mutex testing::internal::g_gmock_mutex
>>> referenced by googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj:(protected: bool __cdecl testing::internal::ExpectationBase::is_retired(void) const)
>>> referenced by googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj:(protected: void __cdecl testing::internal::ExpectationBase::Retire(void))
>>> referenced by googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj:(protected: bool __cdecl testing::internal::ExpectationBase::IsSatisfied(void) const)
>>> referenced 4 more times
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
```
### Steps to reproduce the problem
Ensure you have installed clang correctly, and execute following commands:
```
cmake -B build -G"Ninja" -DBUILD_SHARED_LIBS=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
cmake --build build
```
### What version of GoogleTest are you using?
373af2e3df71599b87a40ce0e37164523849166b
### What operating system and version are you using?
Windows 11 24H2 26100.4351
### What compiler and version are you using?
```
❯ clang -v
clang version 20.1.8
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: D:\Program Files\LLVM\bin
```
### What build system are you using?
```
❯ cmake --version
cmake version 3.31.8
CMake suite maintained and supported by Kitware (kitware.com/cmake).
```
### Additional context
_No response_
Contributor guide
Research direction
Start by reproducing the failure with the provided CMake, Ninja, Clang 20, and BUILD_SHARED_LIBS commands on Windows. Inspect the shared-library build involving googlemock/src/gmock_main.cc and the linker’s missing g_gmock_mutex symbol; done means the build completes successfully without the undefined-symbol error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cmake, cpp
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100