visual-studio cmake RUNTIME_OUTPUT_DIRECTORY 在 Windows 上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/543203/
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
cmake RUNTIME_OUTPUT_DIRECTORY on Windows
提问by thekidder
I'm using cmakefor managing my cross-platform builds, and I have everything worked out except for this problem. I set RUNTIME_OUTPUT_DIRECTORYto a bin/directory where I have data files stored. On Linux, this works fine. On Windows, the executables get placed in the Debug/Releasesub-directory depending on the build type. Is there any way to get cmaketo copy the executable to the proper directory, or (even better) stop using these sub-directories altogether?
我正在使用cmake来管理我的跨平台构建,除了这个问题,我已经解决了所有问题。我设置RUNTIME_OUTPUT_DIRECTORY到bin/我存储数据文件的目录。在 Linux 上,这工作正常。在 Windows 上,可执行文件根据构建类型放置在Debug/Release子目录中。有没有办法让cmake将可执行文件复制到正确的目录,或者(甚至更好)完全停止使用这些子目录?
采纳答案by Christopher Bruns
I've been using the fine prefix property hack reported by Ogapofor years. It works.
多年来,我一直在使用Ogapo报告的精细前缀属性破解。有用。
But as of CMake version 2.8, there is official support for avoiding the Release/Debug subdirectories on Windows.
但是从 CMake 2.8 版开始,官方支持避免 Windows 上的 Release/Debug 子目录。
Use either the global CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION>variables, or the per-target <ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION>properties, like so:
使用全局CMAKE_<ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION>变量或每个目标的<ARTIFACT>_OUTPUT_DIRECTORY_<CONFIGURATION>属性,如下所示:
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${OUTPUT_DIRECTORY}")
SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${OUTPUT_DIRECTORY}")
回答by Ogapo
I am not sure if these directories are intentional or a bug, but at the risk of forward incompatibility you could add:
我不确定这些目录是有意还是错误,但冒着向前不兼容的风险,您可以添加:
if (MSVC_IDE)
    # hack to get around the "Debug" and "Release" directories cmake tries to add on Windows
    set_target_properties (${NAME} PROPERTIES PREFIX "../")
endif()
this has been working for me
这对我有用
回答by Jon Tackabury
I found a few good discussions on this topic:
我发现了一些关于这个主题的很好的讨论:
http://www.cmake.org/pipermail/cmake/2008-April/021355.html
http://www.cmake.org/pipermail/cmake/2008-April/021355.html
http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=8366
http://www.vtk.org/Bug/bug_view_advanced_page.php?bug_id=8366
Would it be possible to use the deprecated EXECUTABLE_OUTPUT_PATHinstead of RUNTIME_OUTPUT_DIRECTORY? I'm not sure what functionality has changed between the 2, but it might be worth a try.
是否可以使用已弃用的EXECUTABLE_OUTPUT_PATH而不是RUNTIME_OUTPUT_DIRECTORY?我不确定 2 之间的哪些功能发生了变化,但可能值得一试。
回答by thekidder
So far, the best answer I've found is to simply write CMake install commandsfor each of my targets and data files, and set up the MSVC debugger to run out of the install directory. This has the added benefit of using CPack for creating installers.
到目前为止,我找到的最佳答案是简单地为我的每个目标和数据文件编写CMake 安装命令,并设置 MSVC 调试器以运行安装目录。这具有使用 CPack 创建安装程序的额外好处。
回答by Mathemaster
For me a global:
对我来说是一个全球性的:
set(CMAKE_STATIC_LIBRARY_PREFIX "../lib")
set(CMAKE_SHARED_LIBRARY_PREFIX "../lib")
did the work, so I don't have to set it for each library (CMake 2.8.4)
完成了工作,所以我不必为每个库(CMake 2.8.4)设置它
回答by verbalshadow
Some cmake variables have build specific versions.
一些 cmake 变量具有特定的构建版本。
CMAKE_C_FLAGS 
    the compiler flags for compiling C sources. Note you can also specify switches with ADD_DEFINITIONS(). 
CMAKE_C_FLAGS_DEBUG 
    compiler flags for compiling a debug build from C sources. 
CMAKE_C_FLAGS_RELEASE 
    compiler flags for compiling a release build from C sources. 
CMAKE_C_FLAGS_RELWITHDEBINFO 
    compiler flags for compiling a release build with debug flags from C sources. 
I have not verified these vars exist, but maybe setting RUNTIME_OUTPUT_DIRECTORY_DEBUG&& RUNTIME_OUTPUT_DIRECTORY_RELEASEto the same thing might work.
我还没有验证这些变量是否存在,但也许将RUNTIME_OUTPUT_DIRECTORY_DEBUG&&设置RUNTIME_OUTPUT_DIRECTORY_RELEASE为相同的东西可能会起作用。
回答by Charlie
You need to change the build location of your visual studio projects. Go to Project Properties and on the Compile tab specify the 'Build output path' to be wherever you wish.
您需要更改 Visual Studio 项目的构建位置。转到“项目属性”并在“编译”选项卡上将“构建输出路径”指定为您希望的任何位置。
Note
笔记
Do not know how relevant this is as I don't know about CMake.
不知道这有多相关,因为我不了解 CMake。
You can use the following token in Visual Studio build events:
您可以在 Visual Studio 生成事件中使用以下令牌:
$(TargetPath)
$(TargetPath)
This will be the path to the location that your project is built to so depending on your project settings this will either be the Debug or Release folder.
这将是您的项目所在位置的路径,因此根据您的项目设置,这将是 Debug 或 Release 文件夹。

