如何在 CMake 中更改 Win32 版本的可执行输出目录?

2021-12-26 00:00:00 visual-studio cmake c++

我的问题是这样的:我正在使用 Visual Studio 2010 开发一个小型解析器.我使用 CMake 作为构建配置工具.

My problem is as such : I'm developing a small parser using Visual Studio 2010. I use CMake as a build configuration tool.

但我发现默认的可执行文件构建行为很不方便.我想要的是,让我的最终程序位于:

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

而不是

E:/parsec/bin/<build-type>/<exe-name>.exe

你会如何使用 CMake 做到这一点?

How would you do that using CMake ?

推荐答案

有几个选项:

  1. 编译后复制可执行文件
  2. 为您的可执行文件自定义输出目录

编译后复制可执行文件

成功构建后,您可以复制可执行文件(请参阅初学者答案),但使用安装目标可能更好:

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:

使用 install 命令指定将被复制到 CMAKE_INSTALL_PREFIX 目录.您可以在 cmake 的命令行(或在 cmake GUI 中)指定 CMAKE_INSTALL_PREFIX.

Use the install command to specify targets (executables, libraries, headers, etc.) which will be copied to the CMAKE_INSTALL_PREFIX directory. You can specify the CMAKE_INSTALL_PREFIX on the commandline of cmake (or in the cmake GUI).

警告:不建议直接在 cmakelists.txt 中设置绝对路径.

Warning: It is not advised to set absolute paths directly in your cmakelists.txt.

使用set_target_properties 自定义RUNTIME_OUTPUT_DIRECTORY

set_target_properties( yourexe PROPERTIES RUNTIME_OUTPUT_DIRECTORY E:/parsec/bin/ )

作为替代,修改CMAKE_RUNTIME_OUTPUT_DIRECTORY 允许您为 cmake 项目中的所有目标指定此项.注意您还要修改 CMAKE_LIBRARY_OUTPUT_DIRECTORY当您构建 dll 时.

As an alternative, modifying the CMAKE_RUNTIME_OUTPUT_DIRECTORY allows you to specify this for all targets in the cmake project. Take care that you modify the CMAKE_LIBRARY_OUTPUT_DIRECTORY as well when you build dlls.

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib )

附加信息:看看这些问题:

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

CMake:根据 CMake 生成的项目中的配置更改 Visual Studio 和 Xcode 可执行文件的名称

如何不添加 Release 或调试到输出路径?

相关文章