Lesson 49 of 50 · c
Cross‑Compilation – Building for Embedded Targets
Duration: 13 mins
Cross‑compilation allows you to build a binary on a host machine for a different architecture (e.g., ARM Cortex‑M, AVR).
Typical toolchain prefix: arm-none-eabi- or avr-.
arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -O2 -c main.c -o main.o
arm-none-eabi-ld -T stm32.ld main.o -o firmware.elf
arm-none-eabi-objcopy -O binary firmware.elf firmware.bin
Key steps:
- Select the right compiler (
*-gcc). - Use a linker script (
.ld) to place sections (.text,.data,.bss) at correct addresses. - Define the entry point (
Reset_Handler). - Flash the binary using tools like
openocdor vendor‑specific utilities.
📚 Example: Minimal AVR Makefile
CC=avr-gcc
CFLAGS=-mmcu=atmega328p -Os -Wall
OBJ=main.o
all: main.hex
main.elf: $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
main.hex: main.elf
avr-objcopy -O ihex -R .eeprom $< $@
clean:
rm -f *.o *.elf *.hex