Build setup
Build using Makefile
Open your project folder. It is better to use
VSCODE.Open
terminalfrom your project folder.Build project using
makecommand.It is very easy this time to build the project using Makefile. You just need to use the
makecommand and nothig.make -j-jis for doing all jobs code at once. It speeds up build time using parallel processing. If you want to donjobs at once, use-jn. For example, executing8tasks at once:make -j8
Tip
Add -Wl,--print-memory-usage to LDFLAGS in Makefile to print memory usage.
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections
LDFLAGS += -Wl,--print-memory-usage
If you want to clean the project, use
make clean.
Build using CMake
Open your project folder. It is better to use
VSCODE.Open
terminalfrom your project folder.Build project using
cmakecommand.mkdir build cd build cmake .. make -j
But this does not generate
.binfile which is needed for flashing the microcontroller. To generate, you need to usearm-none-eabi-objcopy.arm-none-eabi-objcopy -O binary <your_project_name.elf> <your_project_name.bin>We always do this job. So, it is better to generate binary file using CMake.
Add these lines at the bottom of CMakeLists.txt.
# Generate binary add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin COMMAND arm-none-eabi-objcopy -O binary ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.elf ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin DEPENDS ${CMAKE_PROJECT_NAME} COMMENT "Converting ELF to binary" ) # Binary target add_custom_target(bin ALL DEPENDS ${CMAKE_BINARY_DIR}/${CMAKE_PROJECT_NAME}.bin COMMENT "Generating binary file" )
To build using only one line of command, a script file is needed.
Create a
build.shfile at your project folder for compiling and generating binary file.build.sh#!/bin/bash set +e if [[ "$1" == "clean" ]]; then rm -rf build fi if [ ! -d "build" ]; then mkdir build fi cd build cmake .. make -j cd ..
Make
build.shexecutable.chmod +x build.sh
Compile using one line of command.
./build.sh
To clean first then build, run:
./build.sh clean