在 CMake 中,如何解决 Visual Studio 2010 尝试添加的 Debug 和 Release 目录?

2021-12-14 00:00:00 cmake c++ visual-studio-2010

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

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.

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

With a previous version of CMake (2.6) and a previous version of Visual Studio (2008), I used the following:

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 一起使用吗?

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?

推荐答案

这在一定程度上取决于您想要什么,但我建议您查看可用的 目标属性,类似于这个问题.

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.

这在一定程度上取决于您究竟想要什么.对于每个目标,您可以手动设置 library_output_directory 或runtime_output_directory 属性.

It depends a bit on what you want exactly. For each target, you could manually set the library_output_directory or runtime_output_directory properties.

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 )

相关文章