如何将 CMake 输出转换为“bin"目录?

2021-12-26 00:00:00 cmake plugins c++

我目前正在构建一个具有插件结构的项目.我正在使用 CMake 编译项目.插件编译在单独的目录中.我的问题是 CMake 在源的目录结构中编译并保存二进制文件和插件、动态库.如何让 CMake 将文件保存在 ./bin 目录中?

I'm currently constructing a project with a plugin structure. I'm using CMake to compile the project. The plugins are compiled in separate directories. My problem is that CMake compiles and saves the binaries and plugins, dynamic libraries, in the directory structure of the source. How do I make CMake save the files in something like a ./bin directory?

推荐答案

在 Oleg 的回答中,我相信要设置的正确变量是 CMAKE_RUNTIME_OUTPUT_DIRECTORY.我们在根 CMakeLists.txt 中使用以下内容:

As in Oleg's answer, I believe the correct variable to set is CMAKE_RUNTIME_OUTPUT_DIRECTORY. We use the following in our root CMakeLists.txt:

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

您还可以为每个目标指定输出目录:

You can also specify the output directories on a per-target basis:

set_target_properties( targets...
    PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)

在这两种情况下,您都可以将 _[CONFIG] 附加到变量/属性名称,以使输出目录适用于特定配置(配置的标准值为 DEBUGRELEASEMINSIZERELRELWITHDEBINFO).

In both cases you can append _[CONFIG] to the variable/property name to make the output directory apply to a specific configuration (the standard values for configuration are DEBUG, RELEASE, MINSIZEREL and RELWITHDEBINFO).

相关文章