本文介绍了如何在Windows上进行特定于add_custom_command的配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在我的一个项目中运行单元测试,我有一个自定义命令,该命令将可执行文件,库和其他相关文件复制到其他位置,以便它们可以通过特定的设置运行,而不是在特定位置运行他们是建造的。在Linux上,这非常简单。但是在Windows上,由于cmake将配置名称附加到输出目录(我通常会喜欢,但是在这种情况下会搞砸了),因此我遇到了一些麻烦。这使得很难确定到生成的库或可执行文件的路径。例如,如果我有一个自定义命令,该命令只是将可执行文件复制到另一个目录

In order to run the unit tests in one of my projects, I have a custom command which copies the executable, libraries, and other related files to somewhere else so that they can be run with a specific setup rather than running them where they're built. On Linux, this is quite straightforward. But on Windows, I've hit a bit of a snag due to the fact that cmake appends the configuration name to the ouput directories (which I like in general, but it screws up what I'm doing in this case). It makes it hard to determine the paths to the generated libraries or executables. For instance, if I had a custom command which just copied the executable to another directory

set(EXE_PATH "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/exeName${CMAKE_EXECUTABLE_SUFFIX}")
set(NEW_EXE_PATH "${RUN_UNITTESTS_DIR}/exeName${CMAKE_EXECUTABLE_SUFFIX}")

add_custom_command(TARGET unitTests POST_BUILD
                   COMMAND ${CMAKE_COMMAND} ARGS -E copy "${EXE_PATH}" "${NEW_EXE_PATH}")

Windows,因为可执行文件实际上不在 CMAKE_RUNTIME_OUTPUT_DIRECTORY 中。根据配置类型的不同,它位于 $ {CMAKE_RUNTIME_OUTPUT_DIRECTORY} /发布 $ {CMAKE_RUNTIME_OUTPUT_DIRECTORY} / Debug 中。在Linux上,可以通过使用 CMAKE_BUILD_TYPE 并将其添加到路径中来进行简单修复,但这不适用于Windows,因为在Windows上,cmake会生成多个配置而不是只有一个。因此,我想做的是这样的事情

it's going to choke on Windows, because the executable isn't really in CMAKE_RUNTIME_OUTPUT_DIRECTORY. Depending on the configuration type, it's in either ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Release or ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Debug. On Linux, that could be trivially fixed by using CMAKE_BUILD_TYPE and adding it to the path, but that doesn't work with Windows, because on Windows, cmake generates multiple configurations rather than just one. So, what I'd like to be able to do is something like

add_custom_command(TARGET unitTests POST_BUILD
                   debug
                   COMMAND ${CMAKE_COMMAND} ARGS -E copy "${DEBUG_EXE_PATH}" "${DEBUG_NEW_EXE}")

add_custom_command(TARGET unitTests POST_BUILD
                   release
                   COMMAND ${CMAKE_COMMAND} ARGS -E copy "${RELEASE_EXE_PATH}" "${RELEASE_NEW_EXE}")

cmake命令似乎能够做到这一点(例如 target_link_libraries ),但是据我所知, add_custom_target 不能无法提供这种功能。那么,问题是我该怎么做?如何在Windows上使定制命令特定于配置?

and some cmake commands see to be able to do that (e.g. target_link_libraries), but as far as I can tell, add_custom_target doesn't provide that capability. So, the question is how I would do that? How can I make a custom command be configuration-specific on Windows?

推荐答案

这种情况适用于提供的生成器表达式与 add_custom_command

This is a case for the generator expressions provided for use with add_custom_command.

在您的情况下,您希望将已编译exe的完整路径及其文件名附加到目标目录中。这些分别是 $< TARGET_FILE:unitTests> $< TARGET_FILE_NAME:unitTests>

In your case you want the full path to your compiled exe, and also its filename to append to your destination directory. These are $<TARGET_FILE:unitTests> and $<TARGET_FILE_NAME:unitTests> respectively.

您的完整命令为:

add_custom_command(TARGET unitTests POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E
                       copy $<TARGET_FILE:unitTests>
                            ${RUN_UNITTESTS_DIR}/$<TARGET_FILE_NAME:unitTests>)

这篇关于如何在Windows上进行特定于add_custom_command的配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:47