在CMake中的后构建步骤中将目标文件复制到另一个位置 [英] Copy target file to another location in a post build step in CMake

查看:4352
本文介绍了在CMake中的后构建步骤中将目标文件复制到另一个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态库,取决于配置,在CMake脚本中指定的不同的名称:

  set_target_properties $ {name} PROPERTIES OUTPUT_NAME $ {outputName} 64)
set_target_properties($ {name} PROPERTIES DEBUG_OUTPUT_NAME $ {outputName} 64_d)

最终结果是我在发布和调试版本中得到了不同的名称。我想将生成的库复制到不同的目录作为一个后构建步骤,但是CMake-Fu的礼物(?)没有真正对你的微笑。



我尝试过这样做:

  GET_TARGET_PROPERTY(origfile mylibrary LOCATION)
STRING(REGEX REPLACE/ \\\\\origfile $ {origfile})

set(targetfile my_target_path \\ $ {CMAKE_CFG_INTDIR} \\)
STRING(REGEX REPLACE \\\\targetfile $ {targetfile})

add_custom_command(TARGET mylibrary POST_BUILD
COMMAND copy $ {origfile} $ {targetfile}

这对于发布版本来说很好用,但是对于调试源代码不包括我预期的_d。
如何获取目标的输出路径,以便我可以复制该文件?



/ strong>从上面的代码段可以看出,这是目前的Windows / Visual Studio,但我希望这可以在OS X / Xcode / make工作。



注意:我需要将库放在一个额外的目录中,作为依赖于此库的其他项目的输出目录,以便这些项目能够加载库运行。一个可以接受的替代解决方案是能够创建一个自定义目标进行复制,以便其他项目可以依赖于这个项目,而这又依赖于库。

c> c>



add_custom_command(TARGET mylibrary POST_BUILD
COMMAND $ {CMAKE_COMMAND} -E copy $< TARGET_FILE:mylibrary> $ {targetfile}

您也可以直接在目标目录中生成exe,方法是设置target属性 RUNTIME_OUTPUT_DIRECTORY ,而不是复制它。这具有每个配置选项(例如 RUNTIME_OUTPUT_DIRECTORY_DEBUG )。

  set_target_properties mylibrary PROPERTIES 
RUNTIME_OUTPUT_DIRECTORY_DEBUG< debug path>
RUNTIME_OUTPUT_DIRECTORY_RELEASE< release path>


b $ b

有关更多详细信息,请运行:

  cmake --help属性RUNTIME_OUTPUT_DIRECTORY
cmake - -help属性RUNTIME_OUTPUT_DIRECTORY_< CONFIG>

此外,您应该能够为路径分隔符使用正斜杠,即使在Windows上。 p>

I have a dynamic library that gets a different name depending on configuration, specified in the CMake scripts by:

set_target_properties(${name} PROPERTIES OUTPUT_NAME ${outputName}64)
set_target_properties(${name} PROPERTIES DEBUG_OUTPUT_NAME ${outputName}64_d)

The end result is that I get a different name on release and debug builds. I would like to copy the resulting library to a different directory as a post-build step, but the gift(?) of CMake-Fu did not smile upon yours truly.

I have tried doing this:

GET_TARGET_PROPERTY(origfile mylibrary LOCATION)
STRING(REGEX REPLACE "/" "\\\\" origfile ${origfile})

set(targetfile my_target_path\\${CMAKE_CFG_INTDIR}\\)
STRING(REGEX REPLACE "/" "\\\\" targetfile ${targetfile})

add_custom_command(TARGET mylibrary POST_BUILD
    COMMAND copy ${origfile} ${targetfile}
)

This works fine for release builds, but for debug the source does not include the _d that I would have expected. How do I get the output path for the target so that I can copy the file?

Note: As can be seen from the above snippet, this is currently for Windows/Visual Studio, but I would like this to work on OS X / Xcode / make as well.

Note: I need the library to be placed in an extra directory that serves as the output directory for several other projects that depend on this library, so that these projects are able to load the library at runtime. An alternative solution that would be acceptable would be to be able to create a custom target that does the copying, so that the other projects can depend on this project, which in turn depends on the library.

解决方案

Rather than using the obsolete LOCATION property, prefer using generator expressions:

add_custom_command(TARGET mylibrary POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:mylibrary> ${targetfile}
)

You could also just generate the exe in the target directory directly by setting the target property RUNTIME_OUTPUT_DIRECTORY instead of copying it. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

set_target_properties(mylibrary PROPERTIES
                      RUNTIME_OUTPUT_DIRECTORY_DEBUG <debug path>
                      RUNTIME_OUTPUT_DIRECTORY_RELEASE <release path>
)

For further details run:

cmake --help-property "RUNTIME_OUTPUT_DIRECTORY"
cmake --help-property "RUNTIME_OUTPUT_DIRECTORY_<CONFIG>"

Also, you should be able to use forward slashes throughout for path separators, even on Windows.

这篇关于在CMake中的后构建步骤中将目标文件复制到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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