问题描述
我有一个动态库,它根据配置获得不同的名称,在 CMake 脚本中指定:
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)
最终结果是我在发布和调试版本中获得了不同的名称.我想将生成的库复制到不同的目录作为构建后的步骤,但是 CMake-Fu 的礼物(?)并没有真正对你微笑.
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.
我试过这样做:
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.如何获取目标的输出路径以便复制文件?
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?
注意:从上面的代码片段可以看出,这目前适用于 Windows/Visual Studio,但我希望它也适用于 OS X/Xcode/make.
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.
推荐答案
与其使用过时的 LOCATION
属性,不如使用生成器表达式:
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}
)
您也可以通过设置目标属性RUNTIME_OUTPUT_DIRECTORY
而不是复制它来直接在目标目录中生成exe.这具有每个配置选项(例如 RUNTIME_OUTPUT_DIRECTORY_DEBUG
).
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>"
此外,您应该能够在整个路径分隔符中使用正斜杠,即使在 Windows 上也是如此.
Also, you should be able to use forward slashes throughout for path separators, even on Windows.
这篇关于在 CMake 的后期构建步骤中将目标文件复制到另一个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!