From 48b5c3aa4e021d1f8391b72870f86acac2ee1da0 Mon Sep 17 00:00:00 2001 From: Akul Ranjan Date: Tue, 27 Oct 2026 22:57:19 +0530 Subject: [PATCH 1/3] Document static MSVC runtime builds and test them in CI (#1655) 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. --- .github/workflows/build-and-test.yml | 14 +++++++++++++- docs/platform_specific_build_instructions.md | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 5ef8386334..9ac59bfec0 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -56,7 +56,7 @@ jobs: run: ctest -C ${{ matrix.build_type }} -VV msvc: - name: ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.msvc }} + name: ${{ matrix.os }}.${{ matrix.build_type }}.${{ matrix.lib }}.${{ matrix.crt }}.${{ matrix.msvc }} runs-on: ${{ matrix.os }} defaults: run: @@ -73,7 +73,18 @@ jobs: lib: - shared - static + crt: + - MD + - MT + exclude: + # a DLL with its own static CRT can't safely share CRT objects with the exe + - lib: shared + crt: MT include: + - crt: MD + runtime: 'MultiThreaded$<$:Debug>DLL' + - crt: MT + runtime: 'MultiThreaded$<$:Debug>' - msvc: VS-18-2025 os: windows-2025 generator: 'Visual Studio 18 2026' @@ -94,6 +105,7 @@ jobs: -G "${{ matrix.generator }}" -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DBUILD_SHARED_LIBS=${{ matrix.lib == 'shared' }} + '-DCMAKE_MSVC_RUNTIME_LIBRARY=${{ matrix.runtime }}' env: RUNNER_WORKSPACE: ${{ runner.workspace }} diff --git a/docs/platform_specific_build_instructions.md b/docs/platform_specific_build_instructions.md index 5c1439d0a9..67f742a6a2 100644 --- a/docs/platform_specific_build_instructions.md +++ b/docs/platform_specific_build_instructions.md @@ -35,6 +35,14 @@ The `shlwapi` library (`-lshlwapi`) is required to support a call to `CPUInfo` w When using the static library, make sure to add `BENCHMARK_STATIC_DEFINE` under `[Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions]` +By default the library links against the DLL C runtime (`/MD`, `/MDd`). To use the static C runtime (`/MT`, `/MTd`) instead, set CMake's [`CMAKE_MSVC_RUNTIME_LIBRARY`](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html) when configuring. It applies to `benchmark`, `benchmark_main` and the downloaded GoogleTest alike: + +``` +cmake -S . -B build -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$: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. + Can also use the graphical version of CMake: * Open `CMake GUI`. * Under `Where to build the binaries`, same path as source plus `build`. From cd8205c550ecd6534860c9a62ec623ba65f3e05f Mon Sep 17 00:00:00 2001 From: Akul Ranjan Date: Wed, 28 Oct 2026 17:51:34 +0530 Subject: [PATCH 2/3] docs: note CMAKE_MSVC_RUNTIME_LIBRARY needs CMake 3.15+ --- docs/platform_specific_build_instructions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platform_specific_build_instructions.md b/docs/platform_specific_build_instructions.md index 67f742a6a2..171d64aaa5 100644 --- a/docs/platform_specific_build_instructions.md +++ b/docs/platform_specific_build_instructions.md @@ -35,7 +35,7 @@ The `shlwapi` library (`-lshlwapi`) is required to support a call to `CPUInfo` w When using the static library, make sure to add `BENCHMARK_STATIC_DEFINE` under `[Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions]` -By default the library links against the DLL C runtime (`/MD`, `/MDd`). To use the static C runtime (`/MT`, `/MTd`) instead, set CMake's [`CMAKE_MSVC_RUNTIME_LIBRARY`](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html) when configuring. It applies to `benchmark`, `benchmark_main` and the downloaded GoogleTest alike: +By default the library links against the DLL C runtime (`/MD`, `/MDd`). To use the static C runtime (`/MT`, `/MTd`) instead, set CMake's [`CMAKE_MSVC_RUNTIME_LIBRARY`](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html) (CMake 3.15 or newer) when configuring. It applies to `benchmark`, `benchmark_main` and the downloaded GoogleTest alike: ``` cmake -S . -B build -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$:Debug>" From a23d385bda8a932d8793fac5e301a3a2c51e7811 Mon Sep 17 00:00:00 2001 From: Akul Ranjan Date: Fri, 30 Oct 2026 09:42:52 +0530 Subject: [PATCH 3/3] docs: drop CMAKE_CXX_FLAGS /MT note --- docs/platform_specific_build_instructions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/platform_specific_build_instructions.md b/docs/platform_specific_build_instructions.md index 171d64aaa5..c26d43df3e 100644 --- a/docs/platform_specific_build_instructions.md +++ b/docs/platform_specific_build_instructions.md @@ -41,7 +41,7 @@ By default the library links against the DLL C runtime (`/MD`, `/MDd`). To use t cmake -S . -B build -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded$<$: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. +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. Can also use the graphical version of CMake: * Open `CMake GUI`.