2011-10-12 24 views
16

我试图从几年前用Visual Studio 2010构建一个基于CMake的项目,并且遇到了与项目的输出目录有关的问题。 Visual Studio一直非常热衷于在输出二进制文件时添加Debug /和Release /子目录,并且由于各种原因,我一直非常热衷于删除它们 - 现在我正在使用新版本的CMake和新版本的Visual Studio中,CMake中的旧解决方法似乎不再有效,我正在寻找找到“新”方法。在CMake中,我如何解决Visual Studio 2010尝试添加的Debug和Release目录?

使用以前版本的CMake的(2.6)和Visual Studio(2008年)以前的版本,我用了以下内容:

IF(MSVC_IDE) 
    # A hack to get around the "Debug" and "Release" directories Visual Studio tries to add 
    SET_TARGET_PROPERTIES(${targetname} PROPERTIES PREFIX "../") 
    SET_TARGET_PROPERTIES(${targetname} PROPERTIES IMPORT_PREFIX "../") 
ENDIF(MSVC_IDE) 

这工作得很好,但似乎不再做的伎俩。请有人知道一个类似的,但最新的解决方案,将与CMake 2.8.6和Visual Studio 2010一起工作吗?

回答

49

这取决于你想要的东西,但我建议看看可用的target properties,类似于this question

这取决于你想要的东西。对于每个目标,您可以手动设置library_output_directoryruntime_output_directory属性。

if (MSVC) 
    set_target_properties(${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory}) 
    set_target_properties(${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_DEBUG ${youroutputdirectory}) 
    set_target_properties(${targetname} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_RELEASE ${youroutputdirectory}) 
    # etc for the other available configuration types (MinSizeRel, RelWithDebInfo) 
endif (MSVC) 

你也可以对所有分项目做全球范围内,使用这样的事情:

# First for the generic no-config case (e.g. with mingw) 
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${youroutputdirectory}) 
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${youroutputdirectory}) 
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${youroutputdirectory}) 
# Second, for multi-config builds (e.g. msvc) 
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES}) 
    string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) 
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory}) 
    set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory}) 
    set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${youroutputdirectory}) 
endforeach(OUTPUTCONFIG CMAKE_CONFIGURATION_TYPES) 
+2

辉煌,谢谢 - 原来我一直在寻找ARCHIVE_OUTPUT_DIRECTORY_ {配置}特性在目标上。干杯! :) –

+0

我完全一样,它的工作原理,直到例如add_executable(test test.cpp) get_target_property(test_EXE test LOCATION)被调用,它仍然有D:/Codebase/test/bin/ $(Configuration)/test.exe任何想法如何解决这个问题? – choosyg

2

在CMake的当前版本,你可以用生成器表达式为LIBRARY_OUTPUT_DIRECTORY避免配置专用后缀。

我刚加入$<$<CONFIG:Debug>:>,它总是扩大到无。这看起来有点怪异,但它的工作,这不是太奇怪了,你不能用简短的评论解释:

# Use a generator expression so that the specified folder is used directly, without any 
# configuration-dependent suffix. 
# 
# See https://cmake.org/cmake/help/v3.8/prop_tgt/LIBRARY_OUTPUT_DIRECTORY.html 
set_target_properties(library PROPERTIES 
         LIBRARY_OUTPUT_DIRECTORY my/folder/$<$<CONFIG:Debug>:>) 
相关问题