How to generate both .bin and .hex files?

I am using Mbed Studio (version 1.4.3). When I started with Mbed OS (version 6.2.1), it would produce both a .bin and a .hex file when I built my project. But when I updated to Mbed OS 6.15.1, I started getting just the .bin file.

I eventually tracked down the target.OUTPUT_EXT setting, which I could set to “hex” to get a .hex file. That’s fine for my development setup (I’m using a j-Link BASE Compact), but for the other prototypes I actually need the .bin file to download via the bootloader.

Currently, I have to edit my mbed_app.json file to change the “hex” to a “bin”, and rebuild everything. Is there anyway to get it to produce both type of output each time I build?



1 Like

Hello Michael,

There is a utility program called arm-none-eabi-objcopy in the GCC_ARM package which converts a .bin file to a .hex file.
I use the following Mbed-CLI commds put in one shell script to build both .bin and .hex files:

mbed compile -t GCC_ARM -m ${MCU} ${ARGS} --source . --source ${CUSTOM_TARGETS_PATH} ${SRC_LIB_PATH} $CLEAN --profile ${PROF}

${GCC_ARM_PATH}/bin/arm-none-eabi-objcopy -I binary -O ihex ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.bin ./BUILD/${MCU}/GCC_ARM-${PROFILE}/${PROJ_NAME}.hex

I think you can make the arm-none-eabi-objcopy utility run also by editing the CMakeLists.txt file located in the root directory of your project. Unfortunately, I’m not a CMake expert. Maybe @ladislas can help you with that.

Perhaps that helps.

Do you have the .elf file when compiling?

If so, as @hudakz mentioned, you can use objcopy to do what you want:

arm-none-eabi-objcopy -O binary path/to/target.elf path/to/target.bin
arm-none-eabi-objcopy -O ihex   path/to/target.elf path/to/target.hex

This can be added to a script or to your CMake file.

For the latter, you can get some inspiration here:

Heavily inspired and copied from @MultipleMonomials

Thank you, both @hudakz and @lasislas for your quick answers!

I should have mentioned I am running on Windows; but I can certainly create a cmd script to run the
arm-none-eabi-objcopy tool to create a .bin (or .hex) from the elf. But it’s a small pain that I have to go run a separate script after I do a build in Mbed Studio.

I am curious what changed. Since it’s the same version of Studio, it must be something in the build tools in mbed-os. I don’t think Studio uses CMake to run the build (at least, not on Windows). Do you know what Studio does use? I still have both version of mbed-os (6.2.1 and 6.15.1), but when I diff them, there are approximately a million different files. Which one(s) should I look at to see what changed in the build?

Also, is there a way to get Studio to use CMake? Or do you have to build from the command line to do that?

Thanks again for all your help!


I don’t use mbed studio so I can’t say. But running a script is not that bad compared to recompiling.

you can also move to mbed-tools 2 and use cmake and add the script as post command when building

Thanks again for all your help.

Although I am still curious why it used to produce both (but now it does not), I went ahead created a script I can run to produce the .bin from the .elf when I need it (and I left the build producing the .hex, which is that I mostly need while developing).




it is funny because that feature was available only from MbedOS 6.2 to 6.3 because of additional issues around

I hope that will help to understand

BR, Jan

That clears it up–thanks for doing the spelunking I should have done for myself!