C++ 在 CMake 中,如何解决 Visual Studio 2010 尝试添加的 Debug 和 Release 目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7747857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In CMake, how do I work around the Debug and Release directories Visual Studio 2010 tries to add?
提问by Stuart Golodetz
I'm trying to build one of my CMake-based projects from a couple of years ago with Visual Studio 2010 and I'm running into problems to do with the output directory for a project. Visual Studio has always been very keen on adding Debug/ and Release/ subdirectories when outputting binaries, and for various reasons I've always been very keen on removing them - now that I'm using a new version of CMake and a new version of Visual Studio, the old workaround in CMake no longer seems to work, and I'm looking to find out the "new" way of doing it.
我正在尝试使用 Visual Studio 2010 构建我几年前基于 CMake 的项目之一,但我遇到了与项目的输出目录有关的问题。Visual Studio 一直非常热衷于在输出二进制文件时添加 Debug/ 和 Release/ 子目录,出于各种原因,我一直非常热衷于删除它们 - 现在我正在使用新版本的 CMake 和新版本的Visual Studio,CMake 中的旧解决方法似乎不再有效,我正在寻找“新”的方法。
With a previous version of CMake (2.6) and a previous version of Visual Studio (2008), I used the following:
对于以前版本的 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)
This worked fine, but no longer seems to do the trick. Please does anyone know of a similar but more up-to-date workaround that will work with CMake 2.8.6 and Visual Studio 2010?
这工作得很好,但似乎不再奏效。请问有谁知道类似但更新的解决方法可以与 CMake 2.8.6 和 Visual Studio 2010 一起使用吗?
回答by André
It depends a bit on what you want precisely, but I would recommend to take a look at the available target properties, similar to this question.
这在一定程度上取决于您想要什么,但我建议您查看与此问题类似的可用目标属性。
It depends a bit on what you want exactly. For each target, you could manually set the library_output_directoryor runtime_output_directoryproperties.
这在一定程度上取决于你到底想要什么。对于每个目标,您可以手动设置library_output_directory或runtime_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 )
You could also do this globally for all sub-projects, using something like this:
您还可以使用以下方法对所有子项目全局执行此操作:
# 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 )
回答by Tom Seddon
In current versions of CMake you can use a generator expression for LIBRARY_OUTPUT_DIRECTORY
to avoid the configuration-specific suffix.
在当前版本的 CMake 中,您可以使用生成器表达式LIBRARY_OUTPUT_DIRECTORY
来避免配置特定的后缀。
I just added $<$<CONFIG:Debug>:>
, which always expands to nothing, to mine. This looks a bit weird, but it does work, and it's not so weird you can't explain it with a brief comment:
我刚刚添加了$<$<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>:>)
回答by Erik Campobadal
https://cmake.org/cmake/help/latest/prop_tgt/LIBRARY_OUTPUT_DIRECTORY.htmlexplains that:
https://cmake.org/cmake/help/latest/prop_tgt/LIBRARY_OUTPUT_DIRECTORY.html解释说:
Multi-configuration generators (VS, Xcode) append a per-configuration subdirectory to the specified directory unless a generator expression is used.
除非使用生成器表达式,否则多配置生成器(VS、Xcode)会将每个配置的子目录附加到指定目录。
Thus, the only workaround is to use a generator expression. One cool way to do it is by using $<0:> at the end of your path. So if your path is /what/ever/, you will need to replace it with /what/ever/$<0:>.
因此,唯一的解决方法是使用生成器表达式。一种很酷的方法是在路径的末尾使用 $<0:> 。因此,如果您的路径是 /what/ever/,则需要将其替换为 /what/ever/$<0:>。
Example with the target: Nuua and the path: C:/Nuua/bin/
目标示例:Nuua 和路径:C:/Nuua/bin/
set_target_properties(nuua PROPERTIES RUNTIME_OUTPUT_DIRECTORY C:/Nuua/bin/$<0:>)