cancel
Showing results for 
Search instead for 
Did you mean: 

How to create HEX or BIN file for separate flashing?

George Ruinelli
Associate II

Currently the extention only builds an ELF file.

How can I tell cmake to not only build an ELF file but also a HEX or BIN file?

1 ACCEPTED SOLUTION

Accepted Solutions

So, to answer my own question, I got it working by appending the following lines to `CMakeLists.txt` in the project folder:

# Convert output to hex and binary
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
)

# Convert to bin file -> add conditional check?
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
)

Snipped cortesy of https://github.com/MaJerle/stm32-cube-cmake-vscode

View solution in original post

4 REPLIES 4
Andrew Neil
Evangelist III

Add the conversion as a post-build step;

https://stackoverflow.com/questions/19458031/how-to-create-a-executable-hex-from-elf-file-format

 

AndrewNeil_0-1717079820607.png

 

EDIT: sorry, that's for CubeIDE - I guess VSCode has something similar?


@Andrew Neil wrote:

EDIT: sorry, that's for CubeIDE - I guess VSCode has something similar?


Thanks for your reply. But yes, I need it vor VS Code.

Perhaps a command line option to the linker, or use objcopy / objdump to get the output forms you want.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

So, to answer my own question, I got it working by appending the following lines to `CMakeLists.txt` in the project folder:

# Convert output to hex and binary
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.hex
)

# Convert to bin file -> add conditional check?
add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${CMAKE_PROJECT_NAME}.bin
)

Snipped cortesy of https://github.com/MaJerle/stm32-cube-cmake-vscode