Build setup

Buid using Makefile

  1. Open your project folder. It is better to use VSCODE.

  2. Open terminal from your project folder.

  3. Build project using make command.

    It is very easy this time to build the project using Makefile. You just need to use the make command and nothig.

    make -j
    

    -j is for doing all jobs code at once. It speeds up build time using parallel processing. If you want to do n jobs at once, use -jn. For example, executing 8 tasks 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
  1. If you want to clean the project, use make clean.

Build using CMake

  1. Open your project folder. It is better to use VSCODE.

  2. Open terminal from your project folder.

  3. Build project using cmake command.

    mkdir build
    cd build
    cmake ..
    make -j
    

But this does not generate .bin file which is needed for flashing the microcontroller. To generate, you need to use arm-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.

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

  2. Create a build.sh file at your project folder for compiling and generating binary file.

    flash.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 ..
    
  3. Make build.sh executable.

    chmod +x build.sh
    
  4. Compile using one line of command.

    ./build.sh
    

    To clean first then build, run:

    ./build.sh clean