[英]Compile with /MT instead of /MD using CMake
I'm using CMake on windows with the Windows SDK and NMake Makefiles.我在带有 Windows SDK 和 NMake Makefiles 的 Windows 上使用 CMake。
By default it compiles with the /MD
compiler switch.默认情况下,它使用/MD
编译器开关进行编译。
How can I change it to compile with the /MT
switch instead?如何更改它以使用/MT
开关编译?
You can modify the CMAKE_CXX_FLAGS_<Build Type>
and/or CMAKE_C_FLAGS_<Build Type>
variables:您可以修改CMAKE_CXX_FLAGS_<Build Type>
和/或CMAKE_C_FLAGS_<Build Type>
变量:
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
If your CMake flags already contain /MD
, you can ensure that the above commands are executed after the point at which /MD
is inserted (the later addition of /MT
overrides the conflicting existing option), or you can set the flags from scratch:如果您的 CMake 标志已经包含/MD
,您可以确保在插入/MD
的点之后执行上述命令(后来添加的/MT
覆盖了冲突的现有选项),或者您可以从头开始设置标志:
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
Or alternatively, you could replace the existing /MD
and /MDd
values with /MT
and /MTd
respectively by doing something like:或者,您可以通过执行/MTd
分别用/MT
和/MTd
替换现有的/MD
和/MDd
值:
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
CMake finally added proper support for this in version 3.15 with the MSVC_RUNTIME_LIBRARY
target property: CMake 最终在版本 3.15 中使用MSVC_RUNTIME_LIBRARY
目标属性添加了对此的适当支持:
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
project(my_project)
add_executable(foo foo.c)
set_property(TARGET foo PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
You can also specify a global default by setting the CMAKE_MSVC_RUNTIME_LIBRARY
variable instead.您还可以通过设置CMAKE_MSVC_RUNTIME_LIBRARY
变量来指定全局默认值。
It seems that for Visual Studio 15 2017 and CMake 3.12 the way to replace /MD
by /MT
is by adding this snippet to the CMakeLists.txt file:似乎对于Visual Studio 15 2017和CMake 3.12 ,将/MD
替换为/MT
是将此代码段添加到 CMakeLists.txt 文件中:
if(MSVC)
add_compile_options(
$<$<CONFIG:>:/MT> #---------|
$<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
$<$<CONFIG:Release>:/MT> #--|
)
endif()
I found this solution in the official CMake repository: https://gitlab.kitware.com/cmake/cmake/issues/18390我在官方 CMake 存储库中找到了这个解决方案: https : //gitlab.kitware.com/cmake/cmake/issues/18390
查看ucm_set_runtime - 此宏将替换静态或动态运行时的标志 - 要查看效果,请使用ucm_print_flags (也请查看此堆栈溢出问题)。
I have to use set( ... CACHE ... FORCE)
to overwrite MSVC's default cache.我必须使用set( ... CACHE ... FORCE)
来覆盖 MSVC 的默认缓存。
If I do not use this method, MSVC still outputs /MD
options.如果我不使用这种方法,MSVC 仍然会输出/MD
选项。
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
set(${CompilerFlag} "${${CompilerFlag}}" CACHE STRING "msvc compiler flags" FORCE)
message("MSVC flags: ${CompilerFlag}:${${CompilerFlag}}")
endforeach()
Since cmacke 3.15, you can use MSVC_RUNTIME_LIBRARY
.从 cmacke 3.15 开始,您可以使用MSVC_RUNTIME_LIBRARY
。 See https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html见https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.