Skip to main content
George Ruinelli
Associate II
May 30, 2024
Solved

How to create HEX or BIN file for separate flashing?

  • May 30, 2024
  • 3 replies
  • 7189 views

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?

Best answer by George Ruinelli

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

3 replies

Andrew Neil
Super User
May 30, 2024

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?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
George Ruinelli
Associate II
May 30, 2024

@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.

Tesla DeLorean
Guru
May 30, 2024

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Graduate
December 10, 2024

Hello 

I had the same question and I ended up with this path, which I share with you:

From CubeIDE screen with a project OPEN

Untitled1.jpg

 

I select: Project > Properties and then C/C++ Build.

Untitled2.jpg

 

From the drop down of the C/C++build I select <Settings> and then, on the tab <Tool Settings> I select <MCU/MPU post build outputs>.

I check the boxes Convert to binary and Convert to hex

Finish by pressing APPLY AND CLOSE

Untitled3.jpg

 

On the next BUILD, the .hex and .bin files are present inside RELEASE folder of the project. 

Hope this helped.

George Ruinelli
Associate II
December 10, 2024

@Cats You did realise that I ask vor a VS Code and not for an Eclipse solution?

Pavel A.
Super User
December 10, 2024

Need to add something to the build commands defined in the json file? Google for description of that json file.

Graduate
December 10, 2024

Hmmmm  :) Nope!

You are right!

Actually my "discovery" concerns CubeIDE...