Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C Programming Fundamentals

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:

  1. Select the right compiler (*-gcc).
  2. Use a linker script (.ld) to place sections (.text, .data, .bss) at correct addresses.
  3. Define the entry point (Reset_Handler).
  4. Flash the binary using tools like openocd or 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
Previous: Memory Debugging with ValgrindNext: Best Practices & Coding Style for C