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

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

问题描述

我有一个项目,我创建一个静态和动态版本的库。这些工具与静态版本链接,所以没有特殊的DLL需要在最终的系统上运行。



我可以使用/ MD / MT (和相应的调试),在根CMackLists.txt中有一个简单的设置。



例如,强制/ MT我可以使用以下内容:

  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 对象我需要的。可以吗?这将是方便的。



我查看了 set_target_properties(),但似乎不需要 CMAKE_C_FLAGS_< type> 变量,如果我只是设置一个标准的标志,那么它不会对Debug或Release进行特定的修改。



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

  set_target_properties ($ {PROJECT_NAME} PROPERTIES 
COMPILE_FLAGS/ MT

任何解决方案

解决方案

嗯!我得到它的工作!



我发现这个问题有一个可怕的解决方案,将库分成两个目录,并在每个目录中设置set()。这将是有效的,但这将是一个很大的工作。



如何使用CMake在特定的构建配置中为特定目标设置特定的编译器标志? a>



该解决方案发表了一条链接到此问题的评论:



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



其实刚被标记为固定在2013-06-03 12:52!这意味着解决方案在最新的稳定版本的cmake中是不可用的。不过,Brad King和Stepen Kelly所做的工作肯定会很好。它可以从这里找到的每日版本下载:



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



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

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

endfunction()

其中英文的意思是:如果字符串$&CONFIGURATION>等于调试,则输出/ MT之后的d,否则不输出。



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

 项目(wpkg)

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


StaticCompile()

结果完全一样预期没有任何目录或其他技巧!



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


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.

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

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" )

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.

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"
)

Any solution?

解决方案

Well! I got it working!

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.

How can I set specific compiler flags for a specific target in a specific build configuration using CMake?

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

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

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()

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

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()

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

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中,是否可以使用/ MD与/ MT等设置项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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