Msvc static runtime (#2355) * 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. * docs: note CMAKE_MSVC_RUNTIME_LIBRARY needs CMake 3.15+ * docs: drop CMAKE_CXX_FLAGS /MT note --------- Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>
diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 5ef8386..9ac59bf 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml
@@ -56,7 +56,7 @@ 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 @@ 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$<$<CONFIG:Debug>:Debug>DLL' + - crt: MT + runtime: 'MultiThreaded$<$<CONFIG:Debug>:Debug>' - msvc: VS-18-2025 os: windows-2025 generator: 'Visual Studio 18 2026' @@ -94,6 +105,7 @@ -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 5c1439d..c26d43d 100644 --- a/docs/platform_specific_build_instructions.md +++ b/docs/platform_specific_build_instructions.md
@@ -35,6 +35,14 @@ 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) (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$<$<CONFIG:Debug>:Debug>" +``` + +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`.