C++ 如何在 CMake 中更改 Win32 版本的可执行输出目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13556885/
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
How to change the executable output directory for Win32 builds, in CMake?
提问by Takhiarel
My problem is as such : I'm developing a small parser using Visual Studio 2010. I use CMake as a build configuration tool.
我的问题是这样的:我正在使用 Visual Studio 2010 开发一个小型解析器。我使用 CMake 作为构建配置工具。
But I find the default executable building behaviour, inconvenient. What I want is, have my final program be located in :
但是我发现默认的可执行构建行为很不方便。我想要的是,让我的最终程序位于:
E:/parsec/bin/<exe-name>.<build-type>.exe
rather than
而不是
E:/parsec/bin/<build-type>/<exe-name>.exe
How would you do that using CMake ?
你会如何使用 CMake 做到这一点?
回答by André
There are several options:
有几种选择:
- Copy the executable after building
- Customizing the output-directory for your executable(s)
- 构建后复制可执行文件
- 为您的可执行文件自定义输出目录
Copy the executable after building
构建后复制可执行文件
After a succesful build you can copy the executable (see Beginners answer), but perhaps it is nicer to use an install target:
成功构建后,您可以复制可执行文件(请参阅初学者答案),但使用安装目标可能更好:
Use the installcommand to specify targets (executables, libraries, headers, etc.) which will be copied to the CMAKE_INSTALL_PREFIXdirectory. You can specify the CMAKE_INSTALL_PREFIX on the commandline of cmake (or in the cmake GUI).
使用install命令指定将复制到CMAKE_INSTALL_PREFIX目录的目标(可执行文件、库、头文件等)。您可以在 cmake 的命令行(或在 cmake GUI 中)指定 CMAKE_INSTALL_PREFIX。
Customizing the output-directory for your executable(s)
为您的可执行文件自定义输出目录
Warning:It is not advised to set absolute paths directly in your cmakelists.txt.
警告:不建议直接在 cmakelists.txt 中设置绝对路径。
Use set_target_propertiesto customize the RUNTIME_OUTPUT_DIRECTORY
使用set_target_properties定制RUNTIME_OUTPUT_DIRECTORY
set_target_properties( yourexe PROPERTIES RUNTIME_OUTPUT_DIRECTORY E:/parsec/bin/ )
As an alternative, modifying the CMAKE_RUNTIME_OUTPUT_DIRECTORYallows you to specify this for alltargets in the cmake project. Take care that you modify the CMAKE_LIBRARY_OUTPUT_DIRECTORYas well when you build dlls.
作为替代方案,修改CMAKE_RUNTIME_OUTPUT_DIRECTORY允许您为cmake 项目中的所有目标指定此项。请注意,在构建 dll 时也要修改CMAKE_LIBRARY_OUTPUT_DIRECTORY。
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )
Additional info: Take a look at these questions:
附加信息:看看这些问题:
回答by Beginner
Most probably you will need to copy your binaries with a separate custom command which would look similar to this one:
您很可能需要使用单独的自定义命令来复制二进制文件,该命令看起来类似于以下命令:
add_custom_command(target your_target_name
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${EXAMPLE_BIN_NAME} ${PROJECT_BINARY_DIR}/.
)