使用custom_target创建的cmake目标失败

时间:2018-03-11 14:02:50

标签: cmake objcopy

我正在尝试将自定义目标添加到我的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 个答案:

答案 0 :(得分: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)