You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual Studio中通过CMake修改运行时库

How to Force CMake to Use /MT (Static Runtime) Instead of /MD in Visual Studio 2017

I’ve run into this exact issue with VS 2017 and CMake before—those runtime library settings can be stubborn when dependencies get involved. Let’s break down why your current setup isn’t working, and how to fix it properly without hacking CMakeCache.txt.

Why Your Existing Configuration Isn’t Taking Effect

  • Timing is everything: You’re setting the CMAKE_CXX_FLAGS_* variables after some dependency setup, which means later find_package or add_subdirectory calls might overwrite those flags. CMake’s dependency scripts often tweak runtime library settings to match their own builds.
  • Missing C language flags: If your project or any dependencies use C code (like OpenSSL), you need to set the C runtime flags too—otherwise those targets will still use /MD.
  • Boost alignment: While you set Boost_USE_STATIC_RUNTIME ON, if the runtime flags aren’t set early enough, Boost’s find script might still default to dynamic runtime.

Step-by-Step Fix for CMake 3.11

Since you’re stuck on CMake 3.11 (which predates the cleaner CMAKE_MSVC_RUNTIME_LIBRARY variable), here’s the reliable way to enforce /MT/MTd:

1. Move Runtime Flag Settings to the Top

Set the runtime flags immediately after your project command, before any dependency setup. This ensures they have priority over later configuration changes. Update your top-level CMakeLists.txt like this:

cmake_minimum_required (VERSION 3.11)
project ("WindowCMakeProject")
message(STATUS "Using CMake Version " ${CMAKE_VERSION})

# FIRST: Enforce static runtime libraries - do this BEFORE any other setup!
if (WIN32)
    # Override C++ runtime flags
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
    # Don't forget C runtime flags (critical for dependencies like OpenSSL)
    set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MT")
    set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MTd")
endif()

# Now proceed with your regular configuration
set (CMAKE_CXX_STANDARD 17)

# Setup Boost
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
if ( WIN32 )
    add_definitions( -DBOOST_ALL_NO_LIB )
    set(Boost_USE_STATIC_RUNTIME ON) # Keep this aligned with /MT
endif()
find_package(Boost 1.66.0 REQUIRED COMPONENTS system)

# ... rest of your CMake configuration remains the same

2. Override Dependency Targets (If Needed)

Some libraries (like OpenSSL) might still force dynamic runtime even after your global settings. For these, you’ll need to patch their target properties manually:

# Add this AFTER find_package(OpenSSL REQUIRED)
if(WIN32)
    # Force OpenSSL targets to use static runtime
    target_compile_options(OpenSSL::SSL PRIVATE
        $<$<CONFIG:Release>:/MT>
        $<$<CONFIG:Debug>:/MTd>
    )
    target_compile_options(OpenSSL::Crypto PRIVATE
        $<$<CONFIG:Release>:/MT>
        $<$<CONFIG:Debug>:/MTd>
    )
endif()

3. Wipe CMake Cache (Critical!)

Your previous manual edits to CMakeCache.txt leave lingering settings that will override your new configuration. Do a clean slate:

  • Delete the CMakeCache.txt file in your build directory
  • Delete the entire build folder (or the VS solution directory)
  • Regenerate your project with CMake (either via VS’s CMake integration or command line)

Optional: Upgrade CMake for a Cleaner Solution

If you can upgrade to CMake 3.15 or newer, use the official CMAKE_MSVC_RUNTIME_LIBRARY variable—it’s designed exactly for this use case and avoids flag juggling:

cmake_minimum_required (VERSION 3.15)
project ("WindowCMakeProject")

# Global static runtime setting (applies to all targets)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

# ... rest of your configuration stays the same

This variable automatically handles both C and C++ targets, and plays nicer with dependency scripts that respect CMake’s runtime library settings.


内容的提问来源于stack exchange,提问作者cogle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:22:25