CMake构建后事件:复制编译的库 [英] CMake post-build-event: copy compiled libraries

查看:767
本文介绍了CMake构建后事件:复制编译的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的二进制目录结构当前是这样的(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}/
)

当前,有两个问题:


  1. $ {CMAKE_ BUILD_TYPE} 似乎没有定义,至少我在输出窗口中得到了该变量的空字符串。

  2. 是否有可能使该后建立事件更通用?像用某些变量替换实际的dll名称一样?

  1. ${CMAKE_BUILD_TYPE} seems to be not defined, at least I get an empty string for that variable in the output window.
  2. Is there a possibility to make that post-build event more generic? Like replacing the actual dll name with some variable?


推荐答案

您可以通过以下方法使它更通用使用生成器表达式

You can make this more generic by using generator expressions:

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.

参考

  • How to copy DLL files into the same folder as the executable using CMake?

这篇关于CMake构建后事件:复制编译的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆