Skip to content

Msvc static runtime - #2355

Merged
dmah42 merged 4 commits into
google:mainfrom
AkulRanjan:msvc-static-runtime
Oct 30, 2026
Merged

dmah42 merged 4 commits into
google:mainfrom
AkulRanjan:msvc-static-runtime

Conversation

@AkulRanjan

Copy link
Copy Markdown
Contributor

I have fixed #1655 by using CMake's existing CMAKE_MSVC_RUNTIME_LIBRARY setting instead of introducing a new project-specific option. This requires CMake 3.15 or later for deployment.

The MSVC CI job now tests both /MD and /MT configurations. The /MT configuration is restricted to static libraries, since using the static runtime with shared libraries can cause separate DLLs to manage their own runtime heaps. I also encountered __acrt_first_block == header debug assertion failures when testing the shared library and /MT combination locally.

The CI matrix increases from 8 to 12 jobs, with .MD and .MT added to job names to make the runtime configuration explicit.

Testing

I tested locally on Visual Studio 2022 (MSVC 19.44) with CMake 4.4.4 and all six targets use the static runtime in the /MT configuration. The test executable runs without requiring the MSVC runtime DLLs and all 87 tests pass.
However, I have not tested locally on Visual Studio 2026.

AI disclosure

I used an AI coding assistant to help investigate the CMake configuration and work through the implementation. I guided it humanly to execute the changes and I reviewed the resulting changes and tested the static-runtime configuration locally.

Use CMake's standard CMAKE_MSVC_RUNTIME_LIBRARY (policy CMP0091) instead of a
new option. Add a /MT leg for static-library builds to the MSVC CI job.
cmake -S . -B build -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$<CONFIG:Debug>:Debug>"
```

Use this instead of adding `/MT` to `CMAKE_CXX_FLAGS`; CMake selects the runtime from this setting. Combine it with the static library (`BUILD_SHARED_LIBS=OFF`), since a DLL with its own static C runtime can't safely share C runtime objects with the executable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this line? do we say elsewhere in the docs to add /MT? if so maybe just remove that advice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the docs don't actually mention adding /MT, so I removed that part to keep things accurate.

I nonetheless kept the note about limiting /MT to static libraries, since I ran into cross-CRT heap assertion failures when testing it with shared libraries locally.

@dmah42

dmah42 commented Oct 30, 2026

Copy link
Copy Markdown
Member

our branch protection will need updating right after we merge. i'll update it after force merging this.

@dmah42
dmah42 merged commit fbeb9ce into google:main Oct 30, 2026
108 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] MSVC 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease'

2 participants