使用 custom_target 创建的 cmake 目标失败
Posted
技术标签:
【中文标题】使用 custom_target 创建的 cmake 目标失败【英文标题】:cmake target created with custom_target fails 【发布时间】:2018-03-11 14:02:49 【问题描述】:我正在尝试将自定义目标添加到我的 Cmake 项目中,它将从 .elf 输出 .hex 文件。这是我使用的一段脚本:
....
set(EXECUTABLE_OUTPUT_PATH "$PROJECT_SOURCE_DIR/$CMAKE_BUILD_TYPE")
....
target_link_libraries($CMAKE_PROJECT_NAME.elf)
set(COMD "$CMAKE_OBJCOPY -Oihex $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.elf $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.hex")
add_custom_target($CMAKE_PROJECT_NAME.hex DEPENDS $CMAKE_PROJECT_NAME.elf COMMAND $COMD)
建筑最终是这样的:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/captain/test
[100%] Built target test.elf
/bin/sh: 1: arm-none-eabi-objcopy -Oihex /home/captain/test/Release/test.elf /home/captain/test/Release/test.hex: not found
.... blah blah blah ...
直接运行:
arm-none-eabi-objcopy -Oihex /home/captain/test/Release/test.elf /home/captain/test/Release/test.hex
按预期工作。怎么可能?
【问题讨论】:
【参考方案1】:问题是你把命令包装在"
:
set(COMD "$CMAKE_OBJCOPY -Oihex $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.elf $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.hex")
删除周围的"
,它应该可以按预期工作:
set(COMD $CMAKE_OBJCOPY -Oihex $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.elf $EXECUTABLE_OUTPUT_PATH/$CMAKE_PROJECT_NAME.hex)
【讨论】:
以上是关于使用 custom_target 创建的 cmake 目标失败的主要内容,如果未能解决你的问题,请参考以下文章
尽管源和依赖关系已过期,但 Meson custom_target 永远不会执行