【问题标题】:Custom command does not get executed with --target option in cmake自定义命令不会在 cmake 中使用 --target 选项执行
【发布时间】:2019-12-18 22:03:59
【问题描述】:

此问题的后续问题:cmake project build only one specific executable (and its dependencies)

我编写了一个自定义目标,因此每次我在项目中编译某些内容时它都会运行。现在我按照上面的问题调用了一个明确的目标,这个自定义命令不再被执行。 代码:

add_custom_target(
    custom_command
    ALL
    DEPENDS hash.h
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "[INFO] CMake generating special *.h file from custom command magic."
)

我已经尝试删除 ALL 指令,但它没有改变任何东西。

忘了补充:我使用的是从源代码编译的cmake/3.13.4

【问题讨论】:

  • 那么,你运行cmake --build --target custom_command?
  • 并且依赖实现了?以及需要执行什么命令?
  • @arrowd 不,我正在运行cmake --build . --target my_target,但在my_target 内部需要执行此命令,因为我必须生成一个包含在项目源文件之一中的*.h 文件跨度>
  • 没有得到它。我希望在您的代码中出现类似 add_custom_target( Generate_hash_h ALL COMMAND myhashgenerator.script hash.h DEPENDS hash.h WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "[INFO] CMake generating special *.h file from custom command magic." ) 的内容。
  • 是的,如果您发布更多代码,可能会更容易理解。生成标头的脚本在哪里?我在您的 add_custom_target 命令中没有看到它...

标签: cmake cmake-language


【解决方案1】:

很抱歉后来的答案,但工作很疯狂。因此,在我阅读了 @Th.Thielemann 和 @squareskittles 的 cmets 之后,我重新阅读了 cmake 文档并找到了我的解决方案。以下代码是很久以前在cmake/3.0.0 下写的:

add_custom_command( 
    OUTPUT some_header.h
    COMMAND cat ${BISON_HEADER} other_header.h | awk -f some_wak_file.awk > some_header.h
    DEPENDS ${BISON_HEADER}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

add_custom_target(
    custom_command
    ALL
    DEPENDS some_header.h
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMENT "[INFO] CMake generating special *.h file from custom command magic."
)

在再次阅读cmake/3.13 的文档后,很明显它可以写成更简单的形式:

add_custom_target(
    timex_utilities_hash 
    ALL
    COMMAND cat ${BISON_HEADER} other_header.h | awk -f some_wak_file.awk > some_header.h
    DEPENDS ${BISON_HEADER}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

再次感谢@Th。 Thielemann 和 @squareskittles 让患者和 inut 再次检查!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多