Skip to main content
Senior
March 30, 2026
Solved

Build outputs and presets in VS Code

  • March 30, 2026
  • 2 replies
  • 270 views

Hi,

I’m trying to understand how the build setup works in VS Code.

Could you advise where and how to configure VS Code so that .bin and .hex files are always generated when building both Debug and Release configurations?

Is this controlled via CMake, or through one of the .json configuration files?

Whichever approach is correct, could you also share the specific scripts or commands that need to be used?

Thank you :)

 

Best answer by Suess

Similar to @Julien D 's answer, just slightly slimmer. I do like Julien's usage use "arm-none-eabi-objcopy" where mine is abstracts it with "CMAKE_OBJCOPY" .

What it's doing below:

  1. Set the output files name
  2. Create a POST_BUILD custom command to generate the HEX file
  3. Copy the .HEX file to the "output" folder located a few folders up (folder structure below)
    • "<root>/source/Project/build/Debug/MYPROJ.hex"
    • "<root>/output/"  <=== copied here, post build

** Note the "file(MAKE_DIRECTORY...)" command is commented out, as the OS generates it on copy.

###################
# Generate HEX file
set(HEX_FILENAME "MYPROJ-${CMAKE_PROJECT_NAME}-v0.0.1.hex")

add_custom_command(
 TARGET ${CMAKE_PROJECT_NAME}
 POST_BUILD
 COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${HEX_FILENAME}
 # COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
 COMMENT "Generating HEX '${HEX_FILENAME}' for '${CMAKE_PROJECT_NAME}'")

# Copy generated file to project's root "output" folder
# file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../../BLARG)
add_custom_command(
 TARGET ${CMAKE_PROJECT_NAME}
 POST_BUILD
 COMMAND ${CMAKE_COMMAND} -E copy
 "${CMAKE_CURRENT_BINARY_DIR}/${HEX_FILENAME}"
 "${CMAKE_CURRENT_SOURCE_DIR}/../../output/${HEX_FILENAME}"
 COMMENT "Copying HEX file to output folder.")

 

2 replies

Julien D
ST Employee
March 31, 2026

Hi @Ricko,

This can be achieved via CMake yes, put something like this in your CMakeLists.txt and probably adapt it to your project:

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
 add_custom_command(
 TARGET ${YOUR_TARGET_NAME}
 POST_BUILD
 COMMAND ${CMAKE_COMMAND} -E echo Post-build command for Debug: arm-none-eabi-objcopy -O binary $<TARGET_FILE:${YOUR_TARGET_NAME}> "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 COMMAND arm-none-eabi-objcopy -O binary $<TARGET_FILE:${YOUR_TARGET_NAME}> "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
 BYPRODUCTS "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 VERBATIM
 )
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Release")
 add_custom_command(
 TARGET ${YOUR_TARGET_NAME}
 POST_BUILD
 COMMAND ${CMAKE_COMMAND} -E echo Post-build command for Release: arm-none-eabi-objcopy -O binary $<TARGET_FILE:${YOUR_TARGET_NAME}> "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 COMMAND arm-none-eabi-objcopy -O binary $<TARGET_FILE:${YOUR_TARGET_NAME}> "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
 BYPRODUCTS "${CMAKE_BINARY_DIR}/${YOUR_TARGET_NAME}.bin"
 VERBATIM
 )
endif()

If your commands are the same it should be possible to factorize the 2 add_custom_command() in a single one by removing the if/endif statements.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
SuessBest answer
Associate II
March 31, 2026

Similar to @Julien D 's answer, just slightly slimmer. I do like Julien's usage use "arm-none-eabi-objcopy" where mine is abstracts it with "CMAKE_OBJCOPY" .

What it's doing below:

  1. Set the output files name
  2. Create a POST_BUILD custom command to generate the HEX file
  3. Copy the .HEX file to the "output" folder located a few folders up (folder structure below)
    • "<root>/source/Project/build/Debug/MYPROJ.hex"
    • "<root>/output/"  <=== copied here, post build

** Note the "file(MAKE_DIRECTORY...)" command is commented out, as the OS generates it on copy.

###################
# Generate HEX file
set(HEX_FILENAME "MYPROJ-${CMAKE_PROJECT_NAME}-v0.0.1.hex")

add_custom_command(
 TARGET ${CMAKE_PROJECT_NAME}
 POST_BUILD
 COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${CMAKE_PROJECT_NAME}> ${HEX_FILENAME}
 # COMMAND ${CMAKE_OBJCOPY} -O binary ${CMAKE_PROJECT_NAME}.elf ${CMAKE_PROJECT_NAME}.bin
 COMMENT "Generating HEX '${HEX_FILENAME}' for '${CMAKE_PROJECT_NAME}'")

# Copy generated file to project's root "output" folder
# file(MAKE_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../../../BLARG)
add_custom_command(
 TARGET ${CMAKE_PROJECT_NAME}
 POST_BUILD
 COMMAND ${CMAKE_COMMAND} -E copy
 "${CMAKE_CURRENT_BINARY_DIR}/${HEX_FILENAME}"
 "${CMAKE_CURRENT_SOURCE_DIR}/../../output/${HEX_FILENAME}"
 COMMENT "Copying HEX file to output folder.")

 

RickoAuthor
Senior
April 3, 2026

Thank you @Julien D and @Suess