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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Cross‑Compilation – Buil | C Programming Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. C Programming Fundamentals

Lesson 49 of 50

·c

Cross‑Compilation – Building for Embedded Targets

13 mins

Course progress98%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • The **prefix** (`arm-none-eabi-`, `avr-`) selects the target architecture.
  • A **linker script** tells the linker where each section lives in memory.
  • `-mcpu` and `-mthumb` (ARM) control instruction set selection.
  • After building, use a programmer (e.g., `avrdude`, `openocd`) to upload.

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
Make it stickPractice what you've learned1 quiz · 3 practical tests · Optional activities from C Programming FundamentalsBrowse activities
Previous lessonMemory Debugging with ValgrindContinue to lesson 50Best Practices & Coding Style for CBack to C Programming Fundamentals curriculum