xcode 在 CMake 的后期构建步骤中将目标文件复制到另一个位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9994045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:21:20  来源:igfitidea点击:

Copy target file to another location in a post build step in CMake

xcodevisual-studiocmakepost-build-event

提问by villintehaspam

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

我有一个动态库,它根据配置获得不同的名称,在 CMake 脚本中通过以下方式指定:

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.

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

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?

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

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.

注意:从上面的代码片段可以看出,这目前适用于 Windows/Visual Studio,但我希望它也适用于 OS X/Xcode/make。

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.

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

回答by Fraser

Rather than using the obsolete LOCATIONproperty, prefer using generator expressions:

与其使用过时的LOCATION属性,不如使用生成器表达式:

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_DIRECTORYinstead of copying it. This has per-configuration options (e.g. RUNTIME_OUTPUT_DIRECTORY_DEBUG).

您也可以通过设置目标属性RUNTIME_OUTPUT_DIRECTORY而不是复制它来直接在目标目录中生成 exe 。这具有每个配置选项(例如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.

此外,您应该能够在整个路径分隔符中使用正斜杠,即使在 Windows 上也是如此。

回答by sakra

Use generator expressionsin the POST_BUILDcommand instead of manually computing the output path. These are configuration aware. Example:

在命令中使用生成器表达式POST_BUILD而不是手动计算输出路径。这些是配置感知的。例子:

add_custom_command(TARGET mylibrary POST_BUILD 
  COMMAND "${CMAKE_COMMAND}" -E copy 
     "$<TARGET_FILE:mylibrary>"
     "my_target_path/$<CONFIGURATION>/$<TARGET_FILE_NAME:mylibrary>" 
  COMMENT "Copying to output directory")

回答by driedler

The other answers weren't 100% clear to me...

其他答案对我来说不是 100% 清楚......

Say you're building an executable test_base.exe, the following will build the executable then copy the .exe to the base 'build' directory:

假设您正在构建一个可执行文件test_base.exe,以下将构建可执行文件,然后将 .exe 复制到基本的“build”目录:

add_executable(test_base "")
target_sources(test_base
    PRIVATE
        catch_main.cpp
        catch_tests.cpp
        sc_main.cpp
)
target_link_libraries(test_base PRIVATE Catch2 systemc)

add_custom_command(TARGET test_base POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:test_base> ${PROJECT_BINARY_DIR}/test_base.exe
    COMMENT "Created ${PROJECT_BINARY_DIR}/test_base.exe"
)

So, after this runs your project will have:

因此,在此运行后,您的项目将具有:

<project dir>/build/test_base.exe

<project dir>/build/test_base.exe