在发布模式下无法在CMake中用/ MT替换/ MD [英] Replacing /MD with /MT in CMake is not possible in Release mode

查看:266
本文介绍了在发布模式下无法在CMake中用/ MT替换/ MD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++库,我想使用Visual Studio 2017(CMake / Ninja)在发布模式下使用/ MT编译器选项对其进行编译。 这里,我前段时间也问过类似的问题。该问题的答案有所帮助,但导致编译器抱怨(报告警告)有关用/ MT覆盖/ MD的信息。然后,我使用了此解决方案,但是设置CMAKE_CXX_FLAGS_RELEASE对释放模式下的编译器命令行参数。我的意思是以下代码在调试模式下可以很好地工作:

I have a C++ library which I want to compile it using Visual Studio 2017 (CMake/Ninja) with /MT compiler option in Release mode. Here, I asked a similar question some time before. The answer to that question helped but causes the compiler to complain (report warning) about overriding /MD with /MT. Then I used this solution, but setting the CMAKE_CXX_FLAGS_RELEASE has no effect on the compiler command line arguments in Release mode. I mean the following code works well in Debug mode:

set(CompilerFlags
    CMAKE_CXX_FLAGS
    CMAKE_CXX_FLAGS_DEBUG
    CMAKE_CXX_FLAGS_RELEASE)
foreach(CompilerFlag ${CompilerFlags})
    message("before replace: " ${${CompilerFlag}})
    string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
    message("after replace: " ${${CompilerFlag}})
endforeach()

运行CMake的结果将是:

The result of running CMake would be:

before replace: /DWIN32 /D_WINDOWS /W3 /GR /EHsc
after replace: /DWIN32 /D_WINDOWS /W3 /GR /EHsc
before replace: /MDd /Zi /Ob0 /Od /RTC1
after replace: /MTd /Zi /Ob0 /Od /RTC1
before replace: /MD /O2 /Ob2 /DNDEBUG
after replace: /MT /O2 /Ob2 /DNDEBUG

生成结果为:

cl.exe  ... /MTd ...

在发布模式下,结果CMake w的运行应该一样但是,生成的结果将是:

In Release mode the result of running CMake would be the same; however, the result of build would be:

cl.exe  ... /MD ...

如果您知道这样做的正确方法,我将不胜感激。

If you know what's the correct way of doing that, I'll be really appreciate to hear that.

推荐答案

在长期苦苦挣扎后仍然有效

it works after long struggling

TARGET_COMPILE_OPTIONS(${library_name} PRIVATE "/MT$<$<CONFIG:Release>:>")

这篇关于在发布模式下无法在CMake中用/ MT替换/ MD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆