Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running objcopy in CMAKE custom command causes error during make

Tags:

cmake

avr

I'm trying to add a post-build command to a small project which will automatically take my build output file (ELF) and convert it to an Intel HEX format for flashing on a microcontroller.

When I add this command however, the build fails. It repeats the command with all of the CMake variable strings substituted in that is run by the shell and post-fixes it with : not found.

When I run that exact line in the terminal after a normal successful build of the ELF, it works as expected. Is there a gotcha I'm missing somewhere with how CMake handles this?

I've added the target to my CMakeLists.txt as follows:

add_custom_command(
    TARGET ${EXECUTABLE_NAME}
    POST_BUILD
    COMMAND "${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME} ${PROJECT_NAME}.hex"
)

The command ends up resolving to <absolute-path>/avr-objcopy -O ihex test_blink.elf test_blink.hex which I can verify since it's printed by CMake out to the terminal.

like image 226
Shane Snover Avatar asked May 02 '26 14:05

Shane Snover


1 Answers

This string is wrong:

COMMAND "${CMAKE_OBJCOPY} -O ihex ${EXECUTABLE_NAME} ${PROJECT_NAME}.hex"

You should use the ARGS keyword:

COMMAND ${CMAKE_OBJCOPY} ARGS -O ihex ${EXECUTABLE_NAME} ${PROJECT_NAME}.hex
like image 147
Kuznets Avatar answered May 05 '26 10:05

Kuznets