在同一个CMakeLists.txt中,是否可以使用/ MT和其他与/ MD设置项目? [英] Is it possible, in the same CMakeLists.txt, to setup projects with /MT and others with /MD?

查看:3378
本文介绍了在同一个CMakeLists.txt中,是否可以使用/ MT和其他与/ MD设置项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我为其创建一个静态和动态版本的库。这些工具与静态版本链接,因此在最终系统上不需要特殊的DLL来运行它们。

I have a project for which I create a static and a dynamic version of the libraries. The tools are linked against the static version so no special DLLs are required to run them on the final system.

我可以用/ MD / MT (以及相应的调试),在根CMakeLists.txt中有一个简单的集合。

I can setup the entire everything to compile with /MD or /MT (and corresponding debug) with a simple set in the root CMakeLists.txt.

例如,要强制/ MT我可以使用以下命令:

For example, to force /MT I can use the following:

set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /MTd" )
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /MTd" )

然而,这意味着动态库将使用 / MT 这是错误的。是否可以在每个项目的基础上做同样的事情?毕竟,一旦解决方案创建,我可以编辑每个项目并修复 / MD / MT 对象我需要的。可以cmake做到吗?

However, that means the dynamic libraries get compiled with /MT which is wrong. Is it possible to do the same thing on a per project basis? After all, once the solution is created, I can edit each project and fix the /MD and /MT objects to what I need. Can cmake do that? It would be handy.

我看过 set_target_properties(),但似乎没有带code> CMAKE_C_FLAGS_< type> 变量,如果我只是设置一个标准的标志集,它不会特定于Debug或Release。

I looked at the set_target_properties() but that does not seem to take the CMAKE_C_FLAGS_<type> variables and if I just set a standard set of flags it won't be specific to Debug or Release.

以下设置属性,但我似乎没有选择调试和释放选项。

The following does set the property, but I don't seem to have a choice for debug and release options.

set_target_properties( ${PROJECT_NAME} PROPERTIES
    COMPILE_FLAGS "/MT"
)

解决方案?

推荐答案

我得到它工作!

我发现这个问题有一个可怕的解决方案,拆分两个目录中的库,并在每个目录中有set()。这将工作,但它将是相当一些工作。

I found this question which has a terrible solution, splitting the library in two directories and have the set() in each directory. That would work, but it would be quite a bit of work.

该解决方案有一个注释,其中包含此问题的链接:

That solution had a comment with a link to this issue:

http://public.kitware.com/Bug/view.php?id=6493

其实刚刚在2013-06-03 12:52标记为固定的!这意味着解决方案在最新稳定版本的cmake中不可用。然而,布莱德·金和斯蒂芬·凯利的工作是明确工作。它可以从这里找到的每日版本下载:

which actually was just marked as fixed on 2013-06-03 12:52! This means the solution is NOT available in the latest stable version of cmake yet. However, what Brad King and Stepen Kelly worked on is definitively working well. It can be downloaded from the daily builds found here:

http://www.cmake.org/files/dev/?C=M;O=D

使用新命令的方式有点棘手,有我写的:

The way to use the new command is a bit tricky, there is what I wrote:

function(StaticCompile)
    target_compile_options( ${PROJECT_NAME}
        PUBLIC "/MT$<$<STREQUAL:$<CONFIGURATION>,Debug>:d>"
    )
endfunction()

这意味着:如果字符串$< CONFIGURATION>等于Debug,则输出

which in English means: if the string "$<CONFIGURATION>" is equal to "Debug" then output "d" after the "/MT", otherwise output nothing.

然后,任何地方,我有一个目标,需要编译/ MT或/ MTd我使用命令如:

Then, anywhere I have a target that needs to be compiled with /MT or /MTd I use the command as in:

project(wpkg)

add_executable( ${PROJECT_NAME}
    wpkg.cpp
    license.cpp
)

StaticCompile()


$ b b

结果与预期没有任何目录或其他技巧!

The result is exactly as expected without any directory or other tricks!

它适用于我的版本cmake-2.8.11.20130803-gd5dc2-win32-x86。 exe是今天可用的。真的很酷! 8 - )

It worked for me with version cmake-2.8.11.20130803-gd5dc2-win32-x86.exe which is the one available today. Really cool! 8-)

这篇关于在同一个CMakeLists.txt中,是否可以使用/ MT和其他与/ MD设置项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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