问题描述
我项目的二进制目录结构目前是这样的(Windows):
The binary directory structure of my project is currently like this (Windows):
bin/mainProject/{Debug,Release}
bin/library1/{Debug,Release}
bin/library2/{Debug,Release}
...
bin/libraryN/{Debug,Release}
我想将库 library1lib.dll
、... libraryNlib.dll
复制到 bin/mainProject/{Debug,Release} 目录.
I'd like to copy the libraries
library1lib.dll
, ... libraryNlib.dll
to the bin/mainProject/{Debug,Release}
directory once they are build.
对于 CMake,我认为使用构建后事件是可行的,因此我尝试将其添加到每个库的
CMakeLists.txt
:
For CMake, I think this is doable using a post-build event, hence I've tried adding this to each of the libraries'
CMakeLists.txt
:
add_custom_command(TARGET library1 POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/library1lib.dll
${CMAKE_BINARY_DIR}/mainProject/${CMAKE_BUILD_TYPE}/
)
目前有两个问题:
${CMAKE_BUILD_TYPE}
似乎没有定义,至少我在输出窗口中得到了该变量的空字符串.是否有可能使构建后事件更通用?比如用一些变量替换实际的 dll 名称?
${CMAKE_BUILD_TYPE}
seems to be not defined, at least I get an empty string for that variable in the output window.Is there a possibility to make that post-build event more generic? Like replacing the actual dll name with some variable?
推荐答案
您可以使用 生成器表达式:
add_custom_command(
TARGET library1
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:library1>
$<TARGET_FILE_DIR:mainProject>/$<TARGET_FILE_NAME:library1>
)
替代方案
您可以 - 如果每个依赖项都在您的 CMake 项目中构建 - 也只需为所有可执行文件和 DLL 提供一个通用输出路径,例如:
You could - if every dependency is build within your CMake project - also just give a common output path for all executables and DLLs with something like:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Out")
注意:这里需要绝对路径,否则它将相对于每个目标的默认输出路径.并注意配置的子目录是由 CMake 自动附加的.
Note: The absolute path is required here because it would otherwise be relative to each targets default output path. And note that the configuration's sub-directory is appended by CMake automatically.
参考资料
这篇关于CMake post-build-event:复制编译的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!