cmake add_custom_command失败,目标被删除 [英] cmake add_custom_command failure, target gets deleted

查看:103
本文介绍了cmake add_custom_command失败,目标被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CMake构建测试可执行文件。在构建过程中,我想运行可执行文件,该可执行文件将返回测试是否通过。如果没有,我希望构建失败。但是,当我使用 add_custom_command(... POST_BUILD ...)并使用 Makefile 生成器时,测试可执行文件将被删除(此问题的解释: GNU为什么要删除文件)。

I am building a test executable using CMake. During the build process, I would like to run the executable, which returns whether the tests pass or not. If not, I would like the build to fail. However, when I use add_custom_command(... POST_BUILD ... ), and use a Makefile generator, the test executable will be deleted (explain in this question: Why does GNU make delete a file).

是否可以使CMake将可执行文件视为 .PRECIOUS ,或者更改CMakeLists.txt这样,如果测试失败,可执行文件就不会被删除?

Is there a way to have CMake treat the executable as a .PRECIOUS, or otherwise change the CMakeLists.txt such that the executable doesn't get deleted if the tests fail?

作为参考,我的CMakeList.txt如下所示(从实际简化):

For reference, my CMakeList.txt looks like the following (simplified from actual):

add_executable(UnitTest unittest.cpp)
add_custom_command(TARGET UnitTest POST_BUILD COMMAND $<TARGET_FILE:UnitTest>)


推荐答案

我提到的解决方案是使用 add_custom_target ,而不是 add_custom_command 。虽然如果测试失败,它将不会删除可执行文件,如果runUnitTest失败,则整个构建过程也会失败,但是由于专门构建UnitTest目标,因此不会构建此目标。

The solution that I was alluding to was to use add_custom_target instead of add_custom_command. While it will not delete the executable if the test fails and the build process as a whole fails if runUnitTest fails, this target does not get built as a result of building the UnitTest target specifically.

add_executable(UnitTest unittest.cpp)
add_custom_target(runUnitTest UnitTest COMMAND $<TARGET_FILE:UnitTest> DEPENDS UnitTest)

这篇关于cmake add_custom_command失败,目标被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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